我最近启动了一个项目,必须根据 Active Directory 对用户进行身份验证。
使用身份验证模式=“Windows”。我成功登录并检索了用户名。
@Context.User.Identity.Name
现在我想知道如何从登录的用户那里获取 GUID。
我想在我自己的数据库中使用事件目录中的 GUID 来使其成员唯一。
最佳答案
由于您使用的是 .NET 3.5 及更高版本,因此您应该检查 System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关内容:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
基本上,您可以定义域上下文并轻松在 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 中使用用户和组变得非常容易:
关于c# - 在 mvc3 c# 项目中检索 AD 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209978/