【问题标题】:Calling windows authenticated WCF service from c#从 C# 调用 Windows 身份验证的 WCF 服务
【发布时间】:2017-11-17 09:59:12
【问题描述】:

我们有一个经过 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) Unauthorized.

有人可以帮忙吗?

【问题讨论】:

  • 您没有提到该服务现在是否在本地,但也许这个问题可以提供帮助:stackoverflow.com/questions/11408318/…
  • 不,服务不是本地的。它托管在同一域下的不同服务器上。
  • 别介意,您是否有权测试服务或使用服务?还可以启用错误日志记录和 WCF 跟踪以检查是否通过 SOAP 传递了有效凭据。
  • 身份验证是在明文(通过端口 389)还是在安全的 LDAP(端口 636)下完成的?我认为您需要对 636 进行身份验证。
  • 问题当然不在于发布的客户端代码,因为它对我来说逐字逐句。尝试使用具有对服务器/服务的已知访问权限的特定用户名/密码凭据。

标签: c# wcf windows-authentication


【解决方案1】:

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

为了实际实现试试这个:WCF Service, Windows Authentication

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我刚刚检查了自己,根据您的设置,服务器无法识别呼叫者。我想说您宁愿切换到另一个能够使用安全通道的绑定,例如 BasicHttpsBinding。然而,后者需要在服务器 (netsh http add sslcert ...) 上设置 SSL 证书,并且可能需要在客户端 (ServicePointManager.ServerCertificateValidationCallback) 中进行一些验证。 还有一个post 也有同样的问题,但它涉及到 IIS。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 2013-02-22
        • 2018-03-28
        相关资源
        最近更新 更多