使用WCF根据绑定配置使用服务并接收不同的错误

时间:2009-12-16 04:41:38

标签: c# wcf web-services wcf-binding wcf-client

我正在尝试使用WCF使用Intranet Web服务。我通过VS2008中的添加服务引用功能添加了对该服务的引用。这样做时,系统会提示我输入网络凭据以访问我提供的服务并添加了服务引用。

然后我编写了一些我希望失败的代码,因为它不会传递凭据以及服务的调用:

FooServiceClient proxy = new FooServiceClient();
bool isValid = proxy.ValidateBar(baz);

当我使用此代码时,我收到异常:
HTTP请求未经授权,客户端身份验证方案为“Negotiate”。
从服务器收到的身份验证标头为“Basic realm =” Kerberos“'。

当使用下面两个代码示例中的任何一个时,我收到的错误是相同的。

FooServiceClient proxy = new FooServiceClient();
proxy.ClientCredentials.UserName.UserName = "USERNAME";
proxy.ClientCredentials.UserName.Password = "PASSWORD";
bool isValid = proxy.ValidateBar(baz);

FooServiceClient proxy = new FooServiceClient();

NetworkCredential creds = new NetworkCredential("USERNAME", "PASSWORD");

proxy.ClientCredentials.Windows.AllowedImpersonationLevel =
  TokenImpersonationLevel.Identification;
proxy.ClientCredentials.Windows.AllowNtlm = false;
proxy.ClientCredentials.Windows.ClientCredential = creds;

bool isValid = proxy.ValidateBar(baz);

我的直觉告诉我,我的安全模式配置不正确。根据服务器管理器,我尝试绑定的端点是通过SSL查找Basic Http Credential。在阅读WCF-BasicHttp Transport Properties之后,我认为我应该使用这种配置:

<security mode="Transport">
  <transport clientCredentialType="Windows" />
  <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

不幸的是,我继续收到同样的错误。

同样,我确信我的麻烦与我的配置问题有关,因为我以前使用过时的添加Web参考在其他项目中使用此服务。

3 个答案:

答案 0 :(得分:2)

您必须真正了解在另一端配置的端点。如果它是自托管并在SSL下运行,那么它应该是传输,但如果它在带有SSL的IIS下运行,那么它可能是 TransportWithMessageCredentials ,并且传输凭据可能是“无”。

让它正确绑定是非常棘手的。

至于你得到的例外

  

提供的URI方案“https”是   无效;预计'http'。参数   名:

当您使用 TransportCredentialOnly 时,您必须使用HTTP绑定而不是HTTPS,并且我确信您没有将您的端点地址更改为HTTP,因为这不是服务引用。

答案 1 :(得分:1)

您对Intranet方案使用了什么绑定?建议的最佳做法是具有传输安全性和Windows凭据的NetTCP(假设您的所有呼叫者都是具有公司Active Directory中的帐户的Intranet客户端)

这样可以避免任何http / https混乱。

但是,要托管netTcp,您需要WAS(Windows进程激活服务器),它是IIS7的一部分,并且只能在Windows Server 2008(Vista服务器)或2008 R2(Win7服务器)上运行。或者您需要自己托管服务。 NT服务。

许多信息仍然缺失!请相应更新您的问题。谢谢!

答案 2 :(得分:0)

以下WCF 绑定配置最终成为解决方案。

<security mode="Transport">
  <transport clientCredentialType="Basic" proxyCredentialType="None"
     realm="" />
  <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>