2012-02-09 49 views
2

内检索AD信息最近,我开始一个项目,我必须对Active Directory中验证用户身份。一个MVC3 C#项目

使用身份验证模式= “窗口”。我设法登录并检索用户名。

@Context.User.Identity.Name 

现在我不知道我怎么会从已登录用户获得GUID。

我想使用的GUID从Active Directory在我自己的数据库,以使其成员是独一无二的。

+2

刚加入此位为未来的乡亲看到这个线程:使用'objectGuid'为你的密钥(如OP是)是/真的/ *如果你要跟踪的数据库中的记录良好*的做法。 ObjectGuid在森林中是不可变的。如果你在森林中进行跨域移动,那么GUID将保持不变,但SID不会。 – 2012-02-10 17:04:47

回答

2

既然你在.NET 3.5及以上,你应该看看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读全部内容:

基本上,你可以定义域范围内,并可以轻松地查找用户和/或组AD:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name); 

if(user != null) 
{ 
    // do something here ... like grabbing the GUID 
    var userGuid = user.Guid; 
} 

的新S.DS.AM使得它很容易在AD玩弄用户和组: