且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c# 检查用户是否为组成员?

更新时间:2023-11-30 15:17:52

我用这段代码解决了

public bool AuthenticateGroup(string userName, string password, string domain, string group)
    {


        if (userName == "" || password == "")
        {
            return false;
        }

        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password);
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
            SearchResult result = mySearcher.FindOne();

            foreach (string GroupPath in result.Properties["memberOf"])
            {
                if (GroupPath.Contains(group))
                {
                    return true;
                }
            }
        }
        catch (DirectoryServicesCOMException)
        {
        }
        return false;
    }

它对我来说很好用,它可以与不属于域控制器/活动目录的机器一起使用

it works fine for me, and it can be use with a machine not part of the Domain Controller / Active Directory

谢谢大家的帮助