检查用户是否是组的成员

use*_*084 1 c# windows asp.net login active-directory-group

我有一个代码来检查用户是否是组的成员.我在登录时使用此功能.

请注意我有域用户和本地用户,例如.testdomain\administrator和administrator.

这是我使用的代码:

using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./" + userGroupName + ",group"))
{
    foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
    {
        using (DirectoryEntry memberEntry = new DirectoryEntry(member))
        {
            string completeName = memberEntry.Name;
            DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
            if (domainValue != null)
            {
                completeName = domainValue.Name + "\\" + memberEntry.Name;
            }
            Global.logger.Info("completeName from " + userGroupName + " = " + completeName);
            if (userName.Equals(completeName, StringComparison.InvariantCultureIgnoreCase))
            {
                Global.logger.Debug("IsUserPartOfWindowsGroup returned True with username =" + userName + " , UserGroupName = " + userGroupName);
                return true;
            }
        }
    }
    Global.logger.Debug("IsUserPartOfWindowsGroup returned false for username =" + userName + " , UserGroupName = " + userGroupName);
    return false;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但是

DirectoryEntry domainValue = GUIUtility.FindDomain(memberEntry);
Run Code Online (Sandbox Code Playgroud)

正如我所见,它在探查器中花了很多时间.是否有更好/更快的方法来处理这个问题?

public static DirectoryEntry FindDomain(DirectoryEntry memberEntry)
{
    if (memberEntry.Parent != null)
    {
        if (memberEntry.Parent.SchemaClassName.Equals("domain", StringComparison.InvariantCultureIgnoreCase))
        {
            return memberEntry.Parent;
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

其他方式:

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();

Global.logger.Info("result == " + result.Path);
foreach (string GroupPath in result.Properties["memberOf"])
{
    if (GroupPath.Contains(adminGroupName))
    {
        Global.logger.Info(compUsrNameForEncryption + "exists in " + adminGroupName);
    }
}
Run Code Online (Sandbox Code Playgroud)

M.B*_*ock 8

这与我使用的非常接近:

public bool IsUserInGroup(string userName, string groupName)
{
    using (var context = new PrincipalContext(ContextType.Machine))
    {
        using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { SamAccountName = userName }))
        {
            using (var user = searcher.FindOne() as UserPrincipal)
            {
                return user != null && user.IsMemberOf(context, IdentityType.SamAccountName, groupName);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有为本地用户测试它.逻辑在我的域中工作,我刚刚改变,PrincipalContext(ContextType.Machine)所以现在应该看看本地用户.

不要忘记添加引用和使用语句 System.DirectoryServices.AccountManagement