2009-08-31 178 views
3

我在另一臺機器上部署了WCF服務,我想根據WCF服務對客戶端進行身份驗證。WCF服務中的身份驗證

我也做了以下幾件事:

1)在IIS我已經取消選中了匿名訪問,並檢查了「集成Windows Authenfication」複選框。

2)我的Web配置

<authentication mode="Windows" /> 
<bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBind"> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

3)在客戶端,我傳遞如下用戶憑據:

MyServiceClient _client; 

_client = new MyServiceClient(); 

_client.ClientCredentials.Windows.ClientCredential.UserName = "username"; 
_client.ClientCredentials.Windows.ClientCredential.Password = "password"; 
_client.ClientCredentials.Windows.ClientCredential.Domain = "mydomain"; 

我的問題是怎樣可以捕獲用戶名和密碼在服務器端(服務部署的地方)?

我如何可以驗證對用戶憑據傳遞?

目前我使用basichttp結合..這是結合不夠好,支持安全模式?

回答

6

在服務器端,您可以對Active Directory進行身份驗證使用傳遞的Windows憑據,還是那麼你就需要使用備用店處理用戶身份驗證。

你可以在你的服務器端代碼中使用訪問調用者的身份:

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity; 

您還可以檢查是否調用它的Windows憑據的Windows用戶(如您的樣品中)通過檢查

ServiceSecurityContext.Current.WindowsIdentity 

如果是NULL,則沒有Windows憑據都已經過去了 - 否則,你可以使用這個Windows標識,以檢查是誰打來的(名稱等) - 你將無法讀取用戶的密碼,但!你可以檢查他的名字,他屬於哪個組,等等。

要使用Windows/Active Directory的驗證,將clientCredentialType爲 「Windows」。您可能必須切換到wsHttpBinding,或者更好:netTcpBinding(如果您位於防火牆後面的Windows LAN中)。

<bindings> 
    <netTcpBinding> 
    <binding name="WindowsSecured"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Windows" /> 
     </security> 
    </binding> 
    </netTcpBinding> 
</bindings> 

因此,只有在Windows域中註冊的用戶才能調用該服務。任何其他用戶都將被拒絕,並且沒有任何額外的工作。

調用Windows用戶後,可以查看ServiceSecurityContext.Current.WindowsIdentity以獲取有關誰在調用的信息。

有關服務安全上下文或Windows identity上可用內容的詳細信息,請檢查MSDN docs

Marc

+1

Hi Marc, 感謝您的回覆。 我想要安全模型,要求用戶在Windows域中擁有帳戶。 – 2009-08-31 07:13:01

+1

當前示例需要使用SQL Server數據庫來存儲用戶信息,但我不希望這樣,我想根據活動目錄驗證用戶。如果它存在於當前域中或不存在。如果用戶存在,然後在哪個組..等 – 2009-08-31 07:18:58

+0

更新,以顯示如何使用AD授權 – 2009-08-31 07:52:39

0

它看起來像你需要一個自定義的用戶名和密碼驗證程序。有一篇MSDN文章涵蓋了所有步驟:How to: Use a Custom User Name and Password Validator

basicHttpBinding的支持多種安全模式。如果使用重載的構造函數,則可以將您所選的值傳遞給BasicHttpSecurityMode

+0

感謝您的回覆。 實際上,我必須驗證用戶,如果它存在於域中或不是。 像例如我的域名是「abcd」 我的用戶是「user1」 我必須確定服務的地方,我可以陷阱user1並檢查它是否存在於域中。 – 2009-08-31 06:45:23

+0

好的,如果你使用的是直接Windows認證,那麼marc_s的答案就是要走的路。 – bobbymcr 2009-08-31 07:05:07