繁体   English   中英

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

[英]Calling windows authenticated WCF service from c#

我们有一个经过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)未经授权。

有人可以帮忙吗?

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

实际实现试试这个: WCF服务,Windows身份验证

确保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() { }

我只是检查了自己,使用您的设置,服务器无法识别呼叫者。 我会说你宁愿切换到能够使用安全通道的另一个绑定,例如BasicHttpsBinding。 但是,后者需要在服务器上设置SSL证书( netsh http add sslcert ... ),并且可能在客户端( ServicePointManager.ServerCertificateValidationCallback )中进行一些验证。 关于同一事项也有一篇文章 ,但它涉及IIS。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM