首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用C#检查活动目录中是否存在UserID

使用C#检查活动目录中是否存在UserID
EN

Stack Overflow用户
提问于 2010-12-16 03:06:36
回答 2查看 48.7K关注 0票数 27

如何检查Active Directory中是否存在该用户in。

我有LDAP String和UserID,我能找出该UserID是否存在于Active Directory中吗?我将此应用程序用于ASP.NET网络应用程序(.NET 3.5)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-16 04:44:07

您可以这样做(用域替换您要进行身份验证的域或完全删除参数):

代码语言:javascript
复制
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,因为它有一些有趣的方法来验证用户凭据等等。

票数 48
EN

Stack Overflow用户

发布于 2010-12-16 04:44:47

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

代码语言:javascript
复制
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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4453801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档