How to get the current user#39;s Active Directory details in C#(如何在 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"名称即 DOMAINSomeBody
,Somebody
部分称为 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 详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C# 中获取当前用户的 Active Directory 详细信


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01