使用 C# 检查 Active Directory 中是否存在 UserID
- 作者: 我不是律师
- 来源: 51数据库
- 2022-10-21
问题描述
我们如何检查 USERID 是否存在于 Active Directory 中.
How can we check whether the USERID exists in Active Directory or not.
我有 LDAP 字符串和用户 ID,我可以找到该用户 ID 是否存在于 Active Directory 中.我将此用于 ASP.NET Web 应用程序 (.NET 3.5)
I have LDAP String and UserID, can I find whether that UserID exists in Active Directory or not. I am using this for ASP.NET Web Application (.NET 3.5)
推荐答案
您可以执行以下操作(将域替换为您正在验证的域或完全删除参数):
You can do something along the lines of (replacing domain with the domain you're authenticating against or removing the parameter altogether):
public bool DoesUserExist(string userName)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN"))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
{
return foundUser != null;
}
}
}
实现检查用户是否存在.这来自 System.DirectoryServices.AccountManagement 命名空间和程序集.
To achieve checking for if a user exists. This comes from the System.DirectoryServices.AccountManagement namespace and assembly.
您可以在 http://msdn 上找到更多信息.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx
您可能希望更多地了解 PrincipalContext,因为它具有用于验证用户凭据等的有趣方法.
You may want to check more into PrincipalContext as it has interesting methods for authenticating user credentials and such.
- 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 包还原找不到包,没有源
