2013-03-22 56 views
0

我要實現這樣的接口:活動目錄asp.net窗體身份驗證3

interface IMembershipWrapper 
{ 
    Guid GetUserId(); 
    Guid GetUserId(string username, bool userIsOnline); 
    bool ValidateUser(string userName, string password); 
    … 
} 

對Active Directory和使用Unity注入它。

我可能會拋出一個NotImplementedException異常的某些方法,但你認爲這通常是可能的嗎?你推薦什麼策略?

我知道我可以通過web.config配置'active directory asp.net forms authentication',如here所述。不幸的是,這不是一個選項。

+0

凸輪你使用Windows身份驗證? – LiverpoolsNumber9 2013-03-22 15:53:13

回答

3

這應該是完全可能的,無需在web.config中更改您的身份驗證系統。特別是如果你使用.NET 3.5+。看看System.DirectoryServices.AccountManagement

要實現GetUserId(string username, bool userIsOnline)你可能想嘗試這樣的:

public Guid GetUserId(string username, bool userIsOnline) { 
    using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]")) { 
     var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username); 
     if(user != null) 
      return user.Guid.Value; 
     else 
      return null; 
    } 
} 

要在PrinicalContext

public bool ValidateUser(string userName, string password) { 
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]")) 
    { 
     return pc.ValidateCredentials(userName, password); 
    } 
} 

實施ValidateUser(string userName, string password)使用ValidateCredentials()沒有你實現的詳細信息,我不知道怎麼樣去執行GetUserId(),因爲你似乎沒有足夠的信息去Active Directory。

+0

很好的答案。 +1 – LiverpoolsNumber9 2013-03-22 16:36:46

+0

看起來不錯。謝謝。將盡快查看它。 – cs0815 2013-03-22 19:03:14