从c#

时间:2017-11-17 09:59:12

标签: c# wcf windows-authentication

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

<basicHttpBinding>
    <binding textEncoding="utf-8" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
        <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />            
        </security>
    </binding>
</basicHttpBinding>

我正在尝试将测试应用程序中的服务称为

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;
}

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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以从 ServiceReference (您已在应用程序中添加)创建客户端对象,以便调用方法,并在其中提供Windows凭据以访问webservice。< / p>

实际实施请尝试:WCF Service, Windows Authentication

答案 1 :(得分:1)

确保wcf服务中的端点配置如下     <endpoint address="" binding="wsHttpBinding" contract="IService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

确保您调用的方法正在使用模拟,即     [OperationBehavior(Impersonation = ImpersonationOption.Required)] public void TestMethod() { }