2011-09-01 75 views
7

我是调用WCF Web服务的小老鼠,所以希望这是一个简单的问题。当使用.NET 4 winform客户端调用Web服务时,如何将授权方案从匿名更改为NTLM?如何使用NTLM授权方案调用Web服务?

现在我收到异常:HTTP请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的验证头是'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请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的验证头是'NTLM'

我试着通过我传递凭据:

dwSvc.ClientCredentials.Windows.ClientCredential = 
    new System.Net.NetworkCredential("user", "pass", "domain"); 

,也...

dwSvc.ClientCredentials.Windows.ClientCredential = 
    CredentialCache.DefaultNetworkCredentials; 

我涉水通过WCF文档,但...哦...男孩有那里很多。我希望这是一件容易的事情?

在此先感谢。

+0

请参阅http://meta.stackexchange.com/questions/2950/should-hi-thanks -taglines-and-salutations-be-from-posts –

回答

6

设置你的配置绑定 到安全模式= “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> 
+0

我试过使用这种配置,但是,我们的服务器只能通过https访问,所以我用'wsHttpBinding'和'TransportWithMessageCredential'替换了它。它不起作用,抛出''HTTP请求未经客户认证方案'匿名'未经授权。从服务器收到的认证头是'NTLM'。''错误。你有什么建议如何使用'wsHttpBinding'工作? –

+0

我在Visual Studio 2010中得到了这个工作。在Visual Studio的新版本中出现了错误。他们生成不同的配置文件。 –