【问题标题】:How to check the valid user's password matches with active directory如何检查有效用户的密码是否与活动目录匹配
【发布时间】:2014-09-11 06:17:14
【问题描述】:

我正在传递用户名和密码以检查用户是否在 Active Directory 中有效。

这是我的代码:

  private bool ValidUser(string name, string userPwd)
    {

        string UserName = "XXXXXXXXXX";
        string Password = "XXXXXXXXXXXXX";
        DirectoryEntry objRootEntry = new DirectoryEntry("XXXXXXXX.com", UserName, Password);
        DirectorySearcher objADSearcher = new DirectorySearcher(objRootEntry);
        objADSearcher.Filter = ("(&(sAMAccountType=xxxxxxxxx)(samAccountName=" + name + "))");
        SearchResult objResult = objADSearcher.FindOne();
        DirectoryEntry objLoginEntry = (objResult != null) ? objResult.GetDirectoryEntry() : null;          
        if (objLoginEntry != null)
        {
            return true;
        }
        return false;
    }

现在它只检查用户名。我需要检查输入密码(userPwd)是否与活动目录匹配。如何做到这一点。

请帮帮我。

【问题讨论】:

标签: c# asp.net active-directory


【解决方案1】:

//你正在输入密码,而在目录条目中找到就足够了。不用再检查了

查看此详细代码

public bool ValidateUser(string domain, string username, string password,string LdapPath, out string Errmsg)
        {
            Errmsg = "";
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
            try
            {
                // Bind to the native AdsObject to force authentication.
                Object obj = entry.NativeObject;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
                if (null == result)
                {
                    return false;
                }
                // Update the new path to the user in the directory
                LdapPath = result.Path;
                string _filterAttribute = (String)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                Errmsg = ex.Message;                   
                throw new Exception("Error authenticating user." + ex.Message);
            }

        }

【讨论】:

  • 仅供参考 - throw new Exception("Error authenticating user." + ex.Message); 永远不会执行。
  • 我明白了!所做的更改
  • 您是否正在检查输入的密码(userPwd)和活动目录对象的(objLoginEntry)密码?
  • 如果您使用要检查的用户/密码创建的entry 获得了有效的entry.NativeObject,则检查密码
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2020-06-18
相关资源
最近更新 更多