使用 Windows 身份验证时获取 Active Directory 信息? [英] Getting Active Directory Info when using Windows Authentication?

查看:33
本文介绍了使用 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 信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆