使用c#从LDAP获取邮件属性

时间:2017-08-04 06:39:06

标签: c# active-directory ldap

我使用此功能访问LDAP并获取用户的电子邮件地址属性

public string Login(string userName, string password)
{
        DirectoryEntry de = new DirectoryEntry();

        de.Path = "LDAP://000.000.0.00"; // forexample
        de.AuthenticationType = AuthenticationTypes.Secure;
        de.Username = userName;
        de.Password = password;

        DirectorySearcher search = new DirectorySearcher(de);
        search.Filter = "(&(samaccountname=" + userName + "))";
        search.ReferralChasing = ReferralChasingOption.All;

        SearchResult resultCol = search.FindOne();
        string mailProperty="";
        if (resultCol != null)
        {
            if (resultCol.Properties["mail"] != null && resultCol.Properties["mail"].Count > 0)
            {
                mailProperty = resultCol.Properties["mail"][0].ToString();
            }
        }

        return mailProperty;
    }

我没有在返回的属性列表中获取mail属性,即我获得的不包含mail属性的属性列表

  • givenname,samaccountname,cn, pwdlastset,whencreated,badpwdcount
    ,displayname,lastlogon,samaccounttype
    ,countrycode,objectguid,usnchanged
    ,当改变,名称,objectsid
    ,logoncount,badpasswordtime,accountexpires
    ,primarygroupid,objectcategory,userprincipalname
    ,useraccountcontrol,description, dscorepropagationdata,distinguishedname,objectclass,usncreated,lastlogontimestamp,adspath,lastlogoff,instancetype,codepage,sn

1 个答案:

答案 0 :(得分:1)

经过系统管理员调查后,我发现从LDAP用户检索邮件属性必须有两个帐号,正常的活动目录帐号和邮箱帐号(交换),如果用户没有邮箱帐号那么任何LDAP查询不会检索邮件属性