【问题标题】:c# check if the user member of a group?c#检查用户是否是组的成员?
【发布时间】:2011-05-20 22:44:27
【问题描述】:

我有一个代码,用于检查用户是否是 AD 的成员,运行良好,

现在我想添加检查用户是否也是组成员的可能性!

我需要修改什么来实现这一点,我做了一些工作,但它失败了!

这是我的代码:

        //Authenticate a User Against the Directory
        private bool Authenticate(string userName,string password, string domain)
        {

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

            bool authentic = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,userName, password);
                object nativeObject = entry.NativeObject;
                authentic = true;
            }
            catch (DirectoryServicesCOMException) { }
            return authentic;
        }

我想变成这样:

private bool Authenticate(string userName,string password, string domain, string group)

【问题讨论】:

  • 您可能为此任务使用了错误的 API。您是否正在为应用程序编写身份验证代码?如果是这样,应该有一个更简单的 API 可以使用。例如,在 ASP.NET 中,您可以使用 Page.User 对象访问此信息。还是应用程序的目的实际上是查询 Active Directory?

标签: c# authentication active-directory authorization


【解决方案1】:

这在 Windows XP 或更早版本上不可用。

无论如何,为了检查组成员身份,您可以使用以下代码:

bool IsInGroup(string user, string group)
{
    using (var identity = new WindowsIdentity(user))
    {
        var principal = new WindowsPrincipal(identity);
        return principal.IsInRole(group);
    }
}

【讨论】:

  • 我确实在一台不是 DC 成员的机器上使用了不同用户和密码的代码!它有效!
  • 谢谢,但是如何将它集成到我的代码中? “字符串用户”让我感到困惑!还有一些我可以看一下的文档吗?
  • 是域中的用户名。如果机器在同一个域中,它可能只使用用户名,或者它可以使用 user@domaindomain\user
  • 对我来说,这个功能就像一个魅力,我根本不需要对请求的用户进行身份验证。
  • Windows XP 的问题在哪里? new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(group) 可以在 Windows XP 中工作吗?
【解决方案2】:

我用这段代码解决了

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

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

谢谢大家的帮助

【讨论】:

    【解决方案3】:

    在 ASP.Net 中您将使用 Page.User.IsInRole("RoleName") 或在 Windows 中您可以使用 System.Threading.Thread.CurrentPrincipal.IsInRole("RoleName")

    【讨论】:

    • “RoleName”是“组”名称吗?
    • 是的,一个用户可以属于不同的角色(组)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多