使用 Windows 身份验证时获取 Active Directory 信息?
- 作者: 断翅的童话
- 来源: 51数据库
- 2022-10-28
问题描述
我在我的 asp.net MVC 3 应用程序上使用 Windows 身份验证.有没有办法从活动目录中获取用户信息?
I am using Windows authentication on my asp.net MVC 3 app. Is there any way possible to get the users information out of active directory?
我知道我可以使用 User.Name.Identity 并且这适用于登录名.但是如何从活动目录中获取用户的名字、姓氏甚至描述或办公室.这可以通过 .net 实现吗?
I know I can user User.Name.Identity and that works for the login name. But what about getting the Users First Name, Last Name and even the Description or Office all from active directory. Is this possible through .net?
推荐答案
当然!!如果您使用 .NET 3.5 或更高版本,这实际上很容易.
Of course!! If you're using .NET 3.5 or up, it's actually pretty easy.
基本上,使用 System.DirectoryServices.AccoutManagement 命名空间(在此处阅读所有相关信息:管理目录安全主体在 .NET Framework 3.5 中).
Basically, use the System.DirectoryServices.AccoutManagement namespace (read all about it here: Managing Directory Security Principals in the .NET Framework 3.5).
然后:您需要找到"用户并获取其属性 - 使用如下代码:
Then: you need to "find" the user and grab it's properties - use code something like this:
// create domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find the user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "username");
if(user != null)
{
// access the user's properties in a nice, object-oriented way
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
