2011-07-14 53 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