【问题标题】:WCF Security: incorrect credentials getting passed to serviceWCF 安全性:传递给服务的凭据不正确
【发布时间】:2010-11-28 16:03:12
【问题描述】:

我从 ISS 连接到 WCF 服务时遇到问题,它传递的是 IIS 应用程序池凭据而不是我的 Windows 凭据。当我在 VS 中按 F5 在本地运行网站时,它会传入我想要的 Windows 凭据。

我的网站设置为使用 Windows 身份验证,并且匿名身份验证已关闭。

我可以在 Windows 事件查看器中看到它没有使用 Kerberos 连接到 IIS 所在的框,它使用的是 NTLM。但是我可以看到它在从 IIS 转到我的 WCF 服务时使用了 Kerberos:

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.AuthenticationType.ToString() 

我认为它应该在连接到 IIS 框时使用 Kerberos,这样有什么想法会受到赞赏吗?

盒子和用户设置为允许委派,我在我的电脑上启用了 NETTCP 通信等

这是我的主机配置,它使用与 IIS 服务器相同的服务器上的控制台应用程序托管:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="defaultBinding" closeTimeout="02:02:00" openTimeout="02:01:00"
            receiveTimeout="02:10:00" sendTimeout="02:02:00" maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647">
          <security mode="Transport" >
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="defaultClientBehavior">
          <clientCredentials />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceConfigBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization impersonateCallerForAllOperations="true" />
          <serviceCredentials>
            <windowsAuthentication includeWindowsGroups="true" allowAnonymousLogons="false" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceConfigBehavior"
          name="ServiceConfig">
        <endpoint address="" behaviorConfiguration="" binding="netTcpBinding"
            bindingConfiguration="defaultBinding" contract="IServiceConfig">
          <identity>
            <servicePrincipalName value="nettcp/RDM" />
            <dns value="" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
            contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://ServerName:8731/ServiceConfig/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

这是我的客户端配置:

     </clientCredentials>
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <bindings>
   <netTcpBinding>
    <binding name="NetTcpBinding_IServiceConfig" closeTimeout="00:01:00"
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
     transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
     hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
     maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <reliableSession ordered="true" inactivityTimeout="00:10:00"
      enabled="false" />
     <security mode="Transport">
      <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
      <message clientCredentialType="Windows" />
     </security>
    </binding>
   </netTcpBinding>
  </bindings>
        <client>
   <endpoint address="net.tcp://syrwp01:8731/ServiceConfig/"
    behaviorConfiguration="defaultClientBehavior" binding="netTcpBinding"
    bindingConfiguration="NetTcpBinding_IServiceConfig" contract="ServiceReference1.IServiceConfig"
    name="NetTcpBinding_IServiceConfig">
    <identity>
     <servicePrincipalName value="nettcp/RDM" />
    </identity>
   </endpoint>
  </client>
    </system.serviceModel>

下面是调用的服务方法:

    [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]        
    public string PrintMessage(string msg)
    {
        Console.WriteLine(DateTime.Now.ToString());

        WindowsIdentity callerWindowsIdentity = ServiceSecurityContext.Current.WindowsIdentity;
        Console.WriteLine("AuthenticationType: " + OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.AuthenticationType.ToString());
        Console.WriteLine("WindowsIdentity.GetCurrent(): {0}", WindowsIdentity.GetCurrent().Name);

        using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
        {
            Console.WriteLine("WindowsIdentity.GetCurrent(): {0}", WindowsIdentity.GetCurrent().Name);
        }
        Console.WriteLine("Method called successfully!");
    }

【问题讨论】:

    标签: c# .net wcf iis


    【解决方案1】:

    确保您指定

    <system.web>
       <identity impersonate="true" />
       <authentication mode="Windows" />
       <authorization>
          <deny users="?" />
       </authorization>
    </system.web>
    

    这确保不允许匿名登录。

    此外,如果您想将您的信用传递给 WCF 服务,您需要使用委托。在您的网站 web.config 中创建一个行为,如下所示:

    <behaviors>
      <endpointBehaviors>
        <behavior name="DelegationBehavior">
          <callbackDebug includeExceptionDetailInFaults="true" />
          <clientCredentials>
            <windows allowedImpersonationLevel="Delegation" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    

    并通过behaviorConfiguration="DelegationBehavior" 在您的端点中使用它。

    如果这不起作用,请尝试将 &lt;serviceAuthenticationManager authenticationSchemes="IntegratedWindowsAuthentication" /&gt; 添加到 WCF 的 web.config 中的 &lt;serviceBehavior&gt;-Tag。

    别忘了用以下方式装饰你的 WCF 方法:

    [OperationBehavior(Impersonation = ImpersonationOption.Required)]
    

    或者,您也可以通过&lt;serviceBehavior&gt; 中的附加标签来模拟每个呼叫:

    <serviceAuthorization impersonateCallerForAllOperations="true" />
    

    我目前遇到了另一个问题,但我的配置应该适用于您的方案已发布在此处:My Stackoverflow Post

    我知道这是一篇很老的帖子,但希望这对遇到同样问题的人有所帮助。

    【讨论】:

      【解决方案2】:

      听起来像是Double Hop Problem 的案例。在大多数情况下,服务器无法将通过网络接收到的凭据模拟传递给另一台主机。

      这里有一个blog post 更详细地描述了这种现象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 2013-10-06
        • 1970-01-01
        • 2012-05-14
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        相关资源
        最近更新 更多