C#检查,如果一组用户会员吗?用户、会员

由网友(柠檬酸了半颗心)分享简介:我有,我用它来检查,​​如果用户是AD的成员code,工作完美,现在我想补充的可能性,以检查用户是否也有组的成员!我需要做什么修改,以实现这一目标,我做了一些工作,但它失败!所以这里是我的code://验证用户对目录私人布尔验证(用户名字符串,字符串密码,串域){如果(用户名==||密码==){返回false;}布尔正...

我有,我用它来检查,​​如果用户是AD的成员code,工作完美,

现在我想补充的可能性,以检查用户是否也有组的成员!

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

所以这里是我的code:

  //验证用户对目录
        私人布尔验证(用户名字符串,字符串密码,串域)
        {

            如果(用户名==||密码==)
            {
                返回false;
            }

            布尔正宗= FALSE;
            尝试
            {
                的DirectoryEntry条目=新的DirectoryEntry(LDAP://+域名,用户名,密码);
                反对nativeObject = entry.NativeObject;
                正宗= TRUE;
            }
            赶上(DirectoryServicesCOMException){}
            返回正宗的​​;
        }
 

我想让它是这样的:

 私人布尔验证(用户名字符串,字符串密码,串域,弦乐群)
 
免费领取爱企查一年会员 新老用户都可领

解决方案

我解决这个code

 公共BOOL AuthenticateGroup(用户名字符串,字符串密码,串域,弦乐群)
    {


        如果(用户名==||密码==)
        {
            返回false;
        }

        尝试
        {
            的DirectoryEntry条目=新的DirectoryEntry(LDAP://+域名,用户名,密码);
            DirectorySearcher从mySearcher =新DirectorySearcher从(输入);
            mySearcher.Filter =(及(objectClass的=用户)(|(CN =+的userName +)(sAMAccountName赋=+的userName +)));
            信息搜索结果的结果= mySearcher.FindOne();

            的foreach(在result.Properties字符串GroupPath [成员])
            {
                如果(GroupPath.Contains(组))
                {
                    返回true;
                }
            }
        }
        赶上(DirectoryServicesCOMException)
        {
        }
        返回false;
    }
 

它正常工作对我来说,它可以是一台机器的域控制器不是一部分使用/ Active Directory的

感谢大家的帮助,

I have a code that I use to check if the user is member of the AD, worked perfectly,

now I want to add the possibility to check if the user also a member of a group!

what do I need to modify to achieve that, I did some work, but it fails!

so here is my code:

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

I want to make it like this:

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

解决方案

I solve it with this code

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

Thank you all for the help

阅读全文

相关推荐

最新文章