WCF混合身份验证UserName和WIndows

时间:2011-07-14 14:03:10

标签: c# .net wcf web-services authentication

可以使用两种类型的身份验证:wcf中的窗口和用户名,使用消息安全模式和证书进行身份验证。我的UserName身份验证cfg / code看起来:
服务器cfg:

<?xml version="1.0"?>
  <configuration>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceCredentialsBehavior">
                <serviceCredentials>
                    <serviceCertificate findValue="cn=cool" storeName="TrustedPeople" storeLocation="CurrentUser" />
                    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Util.CustomUserNameValidator, Util"  />
                </serviceCredentials>
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ServiceCredentialsBehavior" name="Service">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MessageAndUserName" name="SecuredByTransportEndpoint" contract="IService"/>
        </service>
    </services>
    <bindings>
        <wsHttpBinding>
            <binding name="MessageAndUserName">
                <security mode="Message">
                    <message clientCredentialType="UserName"/>
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client/>
</system.serviceModel>
 <system.web>
    <compilation debug="true"/>
</system.web>
 </configuration>

客户cfg:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="LocalCertValidation">
                <clientCredentials>
                    <serviceCertificate>
                        <authentication certificateValidationMode="PeerTrust" trustedStoreLocation="CurrentUser" />
                    </serviceCertificate>
                </clientCredentials>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService" >
                <security mode="Message">
                    <message clientCredentialType="UserName" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:48097/WCFServer/Service.svc"
                  binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_IService"
                  contract="ServiceReference1.IService"
                  name="WSHttpBinding_IService" behaviorConfiguration="LocalCertValidation">
            <identity>
                <dns value ="cool" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>
</configuration>

要更改什么,服务器要知道访问它的Windows身份?

1 个答案:

答案 0 :(得分:1)

有趣的问题!如果您确实需要混合使用身份验证,则可以尝试将传输集设置为一种身份验证类型,将消息设置为另一种身份验证类型。我不知道这是否可以在实践中发挥作用,但考虑到你可以单独配置它们似乎是合理的:)

您可以看看是否可以为绑定设置类似于以下内容的东西以获取Windows凭据(wsHttpBinding可以处理Windows凭据)。

 <security mode="Transport">
        <transport clientCredentialType="Whatever your authentication method is" />
        <message clientCredentialType="Windows" />
      </security>

如果你试试,请告诉我它是否有效!

编辑:

哦,according to the documentation可以进行混合身份验证。您必须将模式设置为“混合”,因此配置可能如下所示:

 <security mode="mixed">
        <transport clientCredentialType="Whatever your authentication method is" />
        <message clientCredentialType="Windows" />
      </security>

来自文档:

混合安全性。 混合安全性为您提供了两全其美的优势:传输安全性可确保消息的完整性和机密性,而用户凭据和声明则封装在每条消息中,如同消息安全。这允许您使用通过严格的传输安全机制无法实现的各种用户凭证,并利用传输安全性能。