使用 C# 检查 Active Directory 中是否存在 UserID

发布于 2024-10-08 10:53:51 字数 143 浏览 0 评论 0原文

我们如何检查 USERID 是否存在于 Active Directory 中。

我有 LDAP 字符串和用户 ID,我可以查找该用户 ID 是否存在于 Active Directory 中。我将其用于 ASP.NET Web 应用程序 (.NET 3.5)

How can we check whether the USERID exists in Active Directory or not.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-10-15 10:53:51

您可以执行以下操作(将域替换为您要进行身份验证的域或完全删除参数):

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 命名空间和程序集。

您可以在 http://msdn.microsoft.com 找到更多信息/en-us/library/system.directoryservices.accountmanagement.aspx

您可能需要更多地了解PrincipalContext,因为它具有用于验证用户凭据等的有趣方法。

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;
        }
    }
}

To achieve checking for if a user exists. This comes from the System.DirectoryServices.AccountManagement namespace and assembly.

You can find more information at http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx

You may want to check more into PrincipalContext as it has interesting methods for authenticating user credentials and such.

情痴 2024-10-15 10:53:51

我将使用 System.DirectoryServices.AccountManagement 命名空间。

string UserID = "grhm";
bool userExists = false;

using (var ctx = new PrincipalContext(ContextType.Domain))
{
    using (var user = UserPrincipal.FindByIdentity(ctx, UserID))
    {
        if (user != null)
        {
            userExists = true;
            user.Dispose();
        }
    }
}

请参阅 http://msdn.microsoft.com/en-us/library/bb344891 .aspx 了解更多信息

I would use the System.DirectoryServices.AccountManagement namespace.

string UserID = "grhm";
bool userExists = false;

using (var ctx = new PrincipalContext(ContextType.Domain))
{
    using (var user = UserPrincipal.FindByIdentity(ctx, UserID))
    {
        if (user != null)
        {
            userExists = true;
            user.Dispose();
        }
    }
}

See http://msdn.microsoft.com/en-us/library/bb344891.aspx for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文