简体   繁体   English

如何在C#中获取当前用户的Active Directory详细信息

[英]How to get the current user's Active Directory details in C#

I am working on an C# and ASP.Net application, that uses Windows Authentication. 我正在使用一个使用Windows身份验证的C#和ASP.Net应用程序。

ie in Web.config: 即在Web.config中:

<system.web>
    <authentication mode="Windows" />
</system.web>

I want to get details for the current user (full name, email address, etc) from Active Directory. 我想从Active Directory获取当前用户的详细信息(全名,电子邮件地址等)。


I can get their pre Windows 2000 user login name (eg: SOMEDOMAIN\\someuser ) by using 我可以通过使用获得他们的Windows 2000用户登录名(例如: SOMEDOMAIN\\someuser

string username = HttpContext.Current.Request.ServerVariables["AUTH_USER"];

I've worked out the LDAP query for the user, using their current login name (not their pre Windows 2000 user login name): 我已经使用他们当前的登录名(而不是他们的Windows 2000用户登录名)为用户制定了LDAP查询:

DirectorySearcher adSearch = new DirectorySearcher(
        "(userprincipalname=someuser@somedomain.com.au)");
SearchResult adSearchResult = adSearch.FindOne();

However, I don't know how to either search AD for the user using their pre W2K login name, or get their login name in the 'someuser@somedomain.com.au' format. 但是,我不知道如何使用他们的pre W2K登录名为用户搜索AD,或者以'someuser@somedomain.com.au'格式获取他们的登录名。

Any ideas? 有任何想法吗?

The "pre Windows 2000" name ie DOMAIN\\SomeBody , the Somebody portion is known as sAMAccountName. “pre Windows 2000”名称即DOMAIN\\SomeBodySomebody部分称为sAMAccountName。

So try: 所以尝试:

using(DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainController"))
{
   using(DirectorySearcher adSearch = new DirectorySearcher(de))
   {
     adSearch.Filter = "(sAMAccountName=someuser)";
     SearchResult adSearchResult = adSearch.FindOne();
   }
}

someuser@somedomain.com.au is the UserPrincipalName, but it isn't a required field. someuser@somedomain.com.au是UserPrincipalName,但它不是必填字段。

Alan already gave you the right answer - use the sAMAccountName to filter your user. Alan已经给你正确的答案 - 使用sAMAccountName来过滤你的用户。

I would add a recommendation on your use of DirectorySearcher - if you only want one or two pieces of information, add them into the "PropertiesToLoad" collection of the DirectorySearcher . 我将添加一个关于您使用DirectorySearcher的建议 - 如果您只需要一条或两条信息,请将它们添加到DirectorySearcher"PropertiesToLoad"集合中。

Instead of retrieving the whole big user object and then picking out one or two items, this will just return exactly those bits you need. 而不是检索整个大用户对象,然后选择一个或两个项目,这将只返回您需要的那些位。

Sample: 样品:

adSearch.PropertiesToLoad.Add("sn");  // surname = last name
adSearch.PropertiesToLoad.Add("givenName");  // given (or first) name
adSearch.PropertiesToLoad.Add("mail");  // e-mail addresse
adSearch.PropertiesToLoad.Add("telephoneNumber");  // phone number

Those are just the usual AD/LDAP property names you need to specify. 这些只是您需要指定的常用AD / LDAP属性名称。

Add reference to COM "Active DS Type Library" 添加对COM“Active DS Type Library”的引用


            Int32 nameTypeNT4               = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4;
            Int32 nameTypeDN                = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_1779;
            Int32 nameTypeUserPrincipalName = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME;

            ActiveDs.NameTranslate nameTranslate = new ActiveDs.NameTranslate();

            // Convert NT name DOMAIN\User into AD distinguished name 
            // "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com"
            nameTranslate.Set(nameTypeNT4, ntUser);

            String distinguishedName = nameTranslate.Get(nameTypeDN);

            Console.WriteLine(distinguishedName);

            // Convert AD distinguished name "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com" 
            // into NT name DOMAIN\User
            ntUser = String.Empty;
            nameTranslate.Set(nameTypeDN, distinguishedName);
            ntUser = nameTranslate.Get(nameTypeNT4);
            Console.WriteLine(ntUser);

            // Convert NT name DOMAIN\User into AD UserPrincipalName Name.User@Company.com
            nameTranslate.Set(nameTypeNT4, ntUser);
            String userPrincipalName = nameTranslate.Get(nameTypeUserPrincipalName);

            Console.WriteLine(userPrincipalName);

If you're using .NET 3.5 SP1+ the better way to do this is to take a look at the 如果您使用的是.NET 3.5 SP1 +,更好的方法是查看

System.DirectoryServices.AccountManagement namespace.

It has methods to find people and you can pretty much pass in any username format you want and then returns back most of the basic information you would need. 它有查找人的方法,你可以传递任何你想要的用户名格式,然后返回你需要的大部分基本信息。 If you need help on loading the more complex objects and properties check out the source code for http://umanage.codeplex.com its got it all. 如果您需要有关加载更复杂的对象和属性的帮助,请查看http://umanage.codeplex.com的源代码,它就可以了。

Brent 黑雁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM