如何使用NTLM授权方案调用Web服务?

时间:2011-09-01 17:41:10

标签: winforms wcf .net-4.0 tfs

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

现在我得到了例外: HTTP请求未经授权使用客户端身份验证方案“Anonymous”。从服务器收到的身份验证标头是“NTLM”

我的目标是构建一个小工具来帮助我监控TFS 2010的数据仓库和多维数据集。 TFS提供WarehouseControlWebService Web服务。登录到服务器后,我可以在浏览器中通过测试模式调用服务。但是,我试图从我的桌面远程调用相同的Web服务。我的用户帐户位于服务器上的本地Administrators组中。

我使用规范的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文档,但是......哦,男孩......那里有很多。我希望这很容易吗?

提前致谢。

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

你正朝着正确的方向前进。这是一个很好的页面,其中包含您需要的可用身份验证方法的一些示例级别信息:http://man.ddvip.com/web/bsaspnetapp/LiB0087.html。至少该页面应该为您提供更多线索以继续您的努力。