c# - 在 mvc3 c# 项目中检索 AD 信息

标签 c# asp.net-mvc-3 active-directory

我最近启动了一个项目,必须根据 Active Directory 对用户进行身份验证。

使用身份验证模式=“Windows”。我成功登录并检索了用户名。

@Context.User.Identity.Name

现在我想知道如何从登录的用户那里获取 GUID。

我想在我自己的数据库中使用事件目录中的 GUID 来使其成员唯一。

最佳答案

由于您使用的是 .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 中使用用户和组变得非常容易:

关于c# - 在 mvc3 c# 项目中检索 AD 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209978/

相关文章:

c# - 为什么 BackgroundWorker 不需要在 ProgressChanged 事件处理程序中调用?

c# - 在系统帐户下运行 Windows 计划任务时出错

javascript - ActiveDirectory2 和分页

C# ActiveDirectory - 检查对象类

c# - 在 C# 中列出目录时如何过滤文件

c# - 在没有 generic.xaml 的情况下创建自定义控件?

linux - Linux 上的单声道加上 MVC3 : Root directory shows file/folder list on web browser

asp.net - BaseController 在 MVC 中的用途是什么?

c# - 如何加入 Entity Framework

active-directory - Active Directory 内部使用 API 创建/删除用户