使用C#在Active Directory中检查用户名是否存在 [英] Check UserID exists in Active Directory using C#

查看:407
本文介绍了使用C#在Active Directory中检查用户名是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何检查用户标识是否存在于Active Directory或没有。

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

我有LDAP字符串和用户名,我可以发现,用户名是否存在于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.

您可以找到<获得更多的信息href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx">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#在Active Directory中检查用户名是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆