如何获得在C#中当前用户的Active Directory的详细信息 [英] How to get the current user's Active Directory details in C#
问题描述
我工作的一个C#和ASP.Net应用程序,使用Windows身份验证。
I am working on an C# and ASP.Net application, that uses Windows Authentication.
即。在Web.config中:
i.e. in Web.config:
<system.web>
<authentication mode="Windows" />
</system.web>
我想获得的详细信息从Active Directory当前用户(全名,电子邮件地址等)。
I want to get details for the current user (full name, email address, etc) from Active Directory.
我可以得到他们的pre Windows 2000用户登录名(例如: SOMEDOMAIN \ SomeUser的
)使用
I can get their pre Windows 2000 user login name (eg: SOMEDOMAIN\someuser
) by using
string username = HttpContext.Current.Request.ServerVariables["AUTH_USER"];
我已经制定了LDAP查询的用户,使用其当前的登录名(不是他们的pre Windows 2000用户登录名):
I've worked out the LDAP query for the user, using their current login name (not their pre Windows 2000 user login name):
DirectorySearcher adSearch = new DirectorySearcher(
"(userprincipalname=someuser@somedomain.com.au)");
SearchResult adSearchResult = adSearch.FindOne();
不过,我不知道如何可以搜索广告的用户使用他们的pre W2K登录名,或到someuser@somedomain.com.au'格式的登录名。
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的Windows 2000的名称,即 DOMAIN \有人
的有人
部分被称为sAMAccountName赋。
The "pre Windows 2000" name i.e. DOMAIN\SomeBody
, the Somebody
portion is known as sAMAccountName.
因此,尝试:
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是的UserPrincipalName,但它不是一个必填字段。
someuser@somedomain.com.au is the UserPrincipalName, but it isn't a required field.
这篇关于如何获得在C#中当前用户的Active Directory的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!