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# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 创建属性设置器委托 2022-01-01