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

查看:147
本文介绍了获得Active Directory的信息使用Windows身份验证时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用我的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并且工作作为登录名。 但是,我们得到的用户名,姓,甚至说明或Office的所有从Active Directory。通过.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或向上,它实际上是pretty的方便。

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).

然后:你需要找到用户,并抓住它的属性 - 使用code是这样的:

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
}

这篇关于获得Active Directory的信息使用Windows身份验证时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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