我是调用 WCF Web 服务的菜鸟,所以我希望这是一个简单的问题。使用 .NET 4 winform 客户端调用 Web 服务时,如何将授权方案从匿名更改为 NTLM?
现在我遇到了异常:HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证 header 是“NTLM”。
我的目标是构建一个小工具来帮助我监控 TFS 2010 的数据仓库和多维数据集。 TFS 提供 WarehouseControlWebService Web 服务。登录到服务器后,我可以在浏览器中通过测试模式调用该服务。但是,我正在尝试从我的桌面远程调用相同的 Web 服务。我的用户帐户在服务器上的本地管理员组中。
我创建了一个带有规范 Button1 和 TextArea1 的 .NET 4 WinForm。然后,我向 Web 服务添加了一个服务引用,并创造性地将其命名为 ServiceReference1:
Add Service Reference...
http://tfssvr:8080/tfs/TeamFoundation/Administration/v3.0/WarehouseControlService.asmx
这是我背后的代码:
private void button1_Click(object sender, EventArgs e)
{
// Creating a proxy takes about 3-4 seconds
var dwSvc = new ServiceReference1.WarehouseControlWebServiceSoapClient();
// Invoking the method throws an MessageSecurityException
var dwStatus = dwSvc.GetProcessingStatus(null, null, null);
}
我收到 System.ServiceModel.Security.MessageSecurityException:
HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证 header 是“NTLM”。
我尝试通过以下方式传递我的凭据:
dwSvc.ClientCredentials.Windows.ClientCredential =
new System.Net.NetworkCredential("user", "pass", "domain");
并且 ...
dwSvc.ClientCredentials.Windows.ClientCredential =
CredentialCache.DefaultNetworkCredentials;
我正在浏览 WCF 文档,但是……哦,男孩……那里有很多。我希望这很容易?
提前致谢。
最佳答案
设置你的配置绑定(bind)
到安全模式="TransportCredentialOnly"和传输clientCredentialType="Ntlm"
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WarehouseControlWebServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://tfsServer:8080/tfs/TeamFoundation/Administration/v3.0/WarehouseControlService.asmx"
binding="basicHttpBinding" bindingConfiguration="WarehouseControlWebServiceSoap"
contract="TfsWarehouse.WarehouseControlWebServiceSoap" name="WarehouseControlWebServiceSoap" />
</client>
</system.serviceModel>
关于winforms - 如何使用 NTLM 授权方案调用 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7274229/