如何在 C# 中获取当前用户的 Active Directory 详细信息
- 作者: 沖田総悟OkitaSatoru
- 来源: 51数据库
- 2022-10-21
问题描述
我正在开发使用 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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
