使用用户名和密码验证Active Directory用户?

时间:2011-09-01 07:52:36

标签: c#-4.0 active-directory

  

可能重复:
  C# Validate a username and password against Active Directory?

我的API用于验证Active Directory中的用户。我使用以下代码来验证用户,它工作正常,它可以检查用户存在,但我需要验证用户的实际AD登录凭据。这是我的代码。

try
        {
            string adServer = ConfigurationManager.AppSettings["Server"];
            string adDomain = ConfigurationManager.AppSettings["Domain"];
            string adUsername = ConfigurationManager.AppSettings["AdiminUsername"];
            string password = ConfigurationManager.AppSettings["Password"];
            string[] dc = adDomain.Split('.');
            string dcAdDomain = string.Empty;

            foreach (string item in dc)
            {
                if (dc[dc.Length - 1].Equals(item))
                    dcAdDomain = dcAdDomain + "DC=" + item;
                else
                    dcAdDomain = dcAdDomain + "DC=" + item + ",";
            }

            DirectoryEntry de = new DirectoryEntry("LDAP://" + adServer + "/CN=Users," + dcAdDomain, adUsername, password);

            DirectorySearcher ds = new DirectorySearcher(de);

            ds.SearchScope = SearchScope.Subtree;

            ds.Filter = "(&(objectClass=User)(sAMAccountName=" + username + "))";

            if (ds.FindOne() != null)
                return true;
        }
        catch (Exception ex)
        {
            ExLog(ex);
        }
        return false;

问题是,我如何传递文件管理器的密码以及用户名。

请告知。

0 个答案:

没有答案