如何在 C# 中获取当前用户的 Active Directory 详细信息 [英] How to get the current user's Active Directory details in C#

查看:37
本文介绍了如何在 C# 中获取当前用户的 Active Directory 详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用 Windows 身份验证的 C# 和 ASP.Net 应用程序.

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.

我可以使用

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

我已经使用他们当前的登录名(不是他们的 Windows 2000 之前的用户登录名)为用户计算出 LDAP 查询:

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();

但是,我不知道如何使用用户的 W2K 前登录名搜索 AD,或者以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"名称即 DOMAINSomeBodySomebody 部分称为 sAMAccountName.

The "pre Windows 2000" name i.e. DOMAINSomeBody, 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屋!

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