Active Directory的DistinguishedName属性

时间:2013-11-05 16:51:46

标签: c# asp.net .net active-directory

这是我的代码:

public bool ActiveDirectoryAuthenticate(string username, string password)
{
   var result = false;

   using (var entry = new DirectoryEntry("LDAP://*****/DC=******,DC=biz",username,password,AuthenticationTypes.Secure))
   {
       var searcher = new DirectorySearcher(entry){Filter = "objectClass=user"};

       try
       {
           var sr = searcher.FindOne();
           var PathDic = sr.Properties["distinguishedName"][0].ToString();
           result = true;
       }
       catch (Exception exception)
       {
       }
    }

    return result;
}

问题是

sr.Properties["distinguishedName"][0].ToString();

没有返回正确的值。

请帮帮我

2 个答案:

答案 0 :(得分:0)

只是一个想法,但你不需要像这样输入

var PathDic = sr.Properties["distinguishedName"][0].Value.ToString();

答案 1 :(得分:0)

我的问题解决了:

public bool ActiveDirectoryAuthenticate(string username, string password)
    {
        var result = false;
        using (
            var entry = new DirectoryEntry("LDAP://PT/DC=pt,DC=biz", username, password,
                AuthenticationTypes.Secure))
        {
            var searcher = new DirectorySearcher(entry) {Filter = "sAMAccountName=Bank.Members"};

            searcher.PropertiesToLoad.Add("distinguishedName");
            try
            {
                var sr = searcher.FindOne();
                var name = sr.Properties["distinguishedName"][0].ToString();
                result = true;
            }
            catch (Exception exception)
            {


            }
        }

        return result;
    }