Getting Active Directory Info when using Windows Authentication?(使用 Windows 身份验证时获取 Active Directory 信息?)
问题描述
我在我的 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
}
这篇关于使用 Windows 身份验证时获取 Active Directory 信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Windows 身份验证时获取 Active Directory 信息?


基础教程推荐
- 创建属性设置器委托 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01