我正在尝试使用 WCF 使用 Intranet Web 服务。我通过VS2008 中的添加服务引用功能添加了对服务的引用。这样做时,系统提示我输入网络凭据以访问我提供的服务,并添加了服务参考。
然后,我编写了一些我预计会失败的代码,因为它没有与服务调用一起传递凭据:
FooServiceClient proxy = new FooServiceClient();
bool isValid = proxy.ValidateBar(baz);
当我使用此代码时,我收到异常:
HTTP 请求未经授权,客户端身份验证方案“协商”。
从服务器收到的身份验证标头是“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 查找基本 Http 凭据。在阅读了WCF-BasicHttp 传输属性之后,我相信我应该使用这个配置:
<security mode="Transport">
<transport clientCredentialType="Windows" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
不幸的是,我继续收到同样的错误。
同样,我确信我的麻烦与我的配置问题有关,因为我以前在其他项目中使用过时的Add Web Reference使用此服务。