如何检查有效用户';的密码与active directory匹配

本文关键字:密码 active 匹配 directory 何检查 检查 用户 有效 | 更新日期: 2023-09-27 18:21:32

我正在传递用户名和密码,以检查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)是否与Active directory匹配。如何做到这一点。

请帮帮我。

如何检查有效用户';的密码与active directory匹配

//您正在输入密码,而在目录中查找条目就足够了。无需再次检查

检查此详细代码

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