2013-08-16 42 views
2

我正在爲活動目錄驗證的asp.net創建自定義登錄功能。用戶必須只能使用他的用戶名或用戶名和域名(以及這兩種情況下的密碼)進行登錄。獲取通過PrincipalContext選擇的域

代碼:

AuthUser user = Authentication.getDomainAndUserName(givenUsername); 

bool validAccount = false; 

PrincipalContext network = null; 
if (user.domain != "") network = new PrincipalContext(ContextType.Domain, user.domain); 
else network = new PrincipalContext(ContextType.Domain); 

if (UserPrincipal.FindByIdentity(network, IdentityType.SamAccountName, user.username) != null) { 
     validAccount = network.ValidateCredentials(givenUsername, givenPassword, ContextOptions.Negotiate); 
} 

的 「爲AuthUser」 包含用戶名,如果給出的域。現在,如果用戶沒有明確指定域,上面的工作仍然正常。

所以,如果你

new PrincipalContext(ContextType.Domain); 

看來,域名自動設置。

在這種情況下,我該如何找出它使用的域?

回答

0

你總是可以從用戶主要使用的域名從UserPrincipal.FindByIdentity()

+0

返回由 了吧'UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(網絡,IdentityType.SamAccountName,user.username);' '字符串distinguishedName = userPrincipal.DistinguishedName' 現在。然後提取並加入該字符串的所有「DC」部分以獲取完整的域。 – user1557232

+0

什麼是'網絡'? – SearchForKnowledge

+0

如果你仍然需要答案:它在我原來的問題中定義爲「network = new PrincipalContext(ContextType.Domain,user.domain);」 – user1557232