如何使用C#

时间:2016-07-04 05:29:40

标签: c# active-directory

我正在尝试使用C#访问AD用户的“msDS-ResultantPSO”属性值,我在用户上应用了密码策略,并在“msDS-ResultantPSO”属性中显示了该值。现在,我试图获取这个值使用C#以相同的方式获取AD用户的Normal属性,例如“FirstName,LastName,Email ...”。我添加了ResulantPSO属性以及其他正常属性来加载。我的代码带来除“msDS”之外的所有正常属性值-ResultantPSO”。

请允许任何人在这方面帮助我。

1 个答案:

答案 0 :(得分:2)

我必须确保运行我的程序的用户具有相应的AD读取权限,除此之外应该让您关闭:

var ad = new PrincipalContext(ContextType.Domain, _domain, _ldapPathOu);

UserPrincipal user = UserPrincipal.FindByIdentity(ad, username);
DirectoryEntry entry = user.GetUnderlyingObject() as DirectoryEntry;
DirectorySearcher mySearcher = new DirectorySearcher(entry);
SearchResultCollection results;
mySearcher.PropertiesToLoad.Add("msDS-ResultantPSO");

results = mySearcher.FindAll();

if (results.Count >= 1)
{
    string pso = results[0].Properties["msDS-ResultantPSO"][0].ToString();

    //do something with the pso..
    DirectoryEntry d = new DirectoryEntry(@"LDAP://corp.example.com/"+ pso);

    var searchForPassPolicy = new DirectorySearcher(d);
    searchForPassPolicy.Filter = @"(objectClass=msDS-PasswordSettings)";
    searchForPassPolicy.SearchScope = System.DirectoryServices.SearchScope.Subtree;

    searchForPassPolicy.PropertiesToLoad.AddRange(new string[] {"msDS-MaximumPasswordAge"});
    var x = searchForPassPolicy.FindAll();

    var maxAge = (Int64)x[0].Properties["msDS-MaximumPasswordAge"][0];
    var maxPwdAgeInDays = ConvertTimeToDays(maxAge);
}