从c#调用Windows身份验证的WCF服务

Vij*_*ade 6 c# wcf windows-authentication

我们有一个经过Windows身份验证的WCF服务.绑定配置如下.

<basicHttpBinding>
    <binding textEncoding="utf-8" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
        <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />            
        </security>
    </binding>
</basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

我试图从测试应用程序调用该服务,因为,

try
{
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.ReceiveTimeout = new TimeSpan(10, 10, 00);
    binding.SendTimeout = new TimeSpan(10, 10, 00);
    binding.MaxReceivedMessageSize = Int32.MaxValue;
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    EndpointAddress endpoint = new EndpointAddress("ServiceUrl");

    ChannelFactory<ICRMConnectorService> channelFactory = new ChannelFactory<ICRMConnectorService>(binding, endpoint);
    channelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
    var service = channelFactory.CreateChannel();

    service.TestMethod();
}
catch (Exception ex)
{
    throw ex;
}
Run Code Online (Sandbox Code Playgroud)

该调用返回错误,因为远程服务器返回错误:(401)未经授权.

有人可以帮忙吗?

Soh*_*N3N 1

您可以从ServiceReference(已添加到应用程序中)创建一个客户端对象来调用方法,并在其中提供 Windows 凭据来访问 Web 服务。

对于实际实施,请尝试以下操作:WCF 服务、Windows 身份验证