2013-03-22 59 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