2011-07-14 41 views
5

可以使用2种类型的身份验证:wcf中的windows和用户名,使用消息安全模式和证书进行身份验证。我的用户名认证CFG /代码如下:
服务器CFG:
WCF混合身份验证用户名和WIndows

<?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

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

你可以看看你是否可以设置类似于下面的绑定来获取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> 

从文档:

混合安全。混合安全性为您提供了两全其美的解决方案:传输安全性确保消息的完整性和机密性,而用户证书和声明则封装在每条消息中,就像消息安全中一样。这使您可以使用严格的传输安全机制无法实现的各种用户凭证,并可以充分利用传输安全的性能。

+0

不,它不起作用。 也许我不正确设置Windows凭据,以及如何从服务器检索它们? – croisharp

+0

你真的需要2种不同的身份验证方法吗?您可以将客户端的Windows凭据传递给服务,并允许该服务[模拟用户](http://www.danrigsby.com/blog/index.php/2008/04/17/impersonate-a-clients-身份在-WCF /)。这可能比尝试混合和匹配身份验证类型更好。如果要验证用户名,可以将它们存储在数据库中,并将它们与'WindowsIdentity.GetCurrent()'返回的对象中模拟的客户端用户的详细信息进行比较(最好存储sid,因为可以更改用户名) – Franchesca

+0

是的,我需要2身份验证。方法+与NetSqlAzMan授权,但我知道所有如何做,这是我的问题与混合认证.. – croisharp