c# - 将 Windows 凭据传递到远程 https WCF 服务

标签 c# wcf web-config credentials

我需要一些帮助,我正在尝试将 Windows 凭据传递给 WCF 服务。在 IIS 中,只有 Windows 身份验证为这些服务启用并通过 https 运行。

服务器端配置是:

<system.serviceModel>
<protocolMapping>
  <add scheme="https" binding="basicHttpBinding" bindingConfiguration="httpsBinding"/>
</protocolMapping>
<bindings>
  <basicHttpBinding>
    <binding name="httpsBinding">
      <security mode="Transport">
        <transport clientCredentialType="Windows"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

在客户端:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IMyService" maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647">
      <security mode="Transport">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://myserver.net:4343/MyService.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
    contract="MyServiceReference.IMyService" name="BasicHttpBinding_IMyService" />
</client>

我正在尝试以这种方式使用服务:

Client = new MyServiceClient();
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferPoolSize = long.MaxValue;
binding.MaxBufferSize = int.MaxValue;

EndpointAddress ep = new EndpointAddress("https://myserver.net:4343/MyService.svc");
Client = new COMINTSServiceClient(binding, ep);
Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification;
Client.ClientCredentials.Windows.ClientCredential =  System.Net.CredentialCache.DefaultNetworkCredentials;
Client.Open();
Array[] obj = Client.RandomMethod();

这段代码对我不起作用:

    Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Identification;
    Client.ClientCredentials.Windows.ClientCredential =  System.Net.CredentialCache.DefaultNetworkCredentials;

在服务中,当询问使用 ServiceSecurityContext.Current.WindowsIdentity.Name 调用服务的用户时,总是会得到:ISS APPPOOL\ASP.NET v4.0 而不是调用的域\用户


让它工作的唯一方法是写入用户名和密码而不是 DefaultNetworkCredentials。

Client.ClientCredentials.Windows.ClientCredential.UserName = "DOMAIN\\user";
Client.ClientCredentials.Windows.ClientCredential.Password = "passw";

但我不想硬编码用户/密码。 有什么帮助吗?

最佳答案

尝试:

Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

保留来自 CredentialCache 的分配。

关于c# - 将 Windows 凭据传递到远程 https WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223163/

相关文章:

WCF:如何将自定义 ServiceBehavior 添加到 WCF 配置中

尝试访问 IIS 上的 .NET WCF Web 服务时发生 Java 套接字异常

c# - 反射相关 我牺牲了多少速度?

c# - 使用 Jet 读取 CSV 文件 - 制表符分隔不起作用!

vb.net - 从 Silverlight 4 调用 WCF 服务需要指导

visual-studio-2010 - XDT 配置转换 - ReplaceAll?

.net - 我如何告诉 -all- .NET Trace Sources 使用监听器?

c# - HTTP 错误 404.13 - 未找到,在 asp.net MVC 中上传大文件时

c# - 在 Microsoft.Owin.Security.OpenIdConnect 中间件的响应中添加位置 header

c# - 从存储过程返回值作为输出参数与 SELECT 语句