从Active Directory获取用户时出错:“从服务器返回了引荐”

时间:2019-01-10 09:36:31

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

在此过程中,我使用了主体上下文。在此过程中发生错误。

  

(“从服务器返回了引用”。)

代码如下所示:

注意:我的AD管理员帐户用户拥有所有权限。

var result = new List<string>();
try
{
    var adSettings = SystemSettings.InitializeADFromConfigSection();

    using (var pc = new PrincipalContext(ContextType.Domain, adSettings.AD_IP, "DV=" + adSettings.AD_DV + ",DC=" + adSettings.AD_DC + "", adSettings.AD_UserName, adSettings.AD_Password))
    {
        using (var searcher = new PrincipalSearcher(new UserPrincipal(pc)))
        {
            foreach (Principal item in searcher.FindAll())
            {
                DirectoryEntry de = item.GetUnderlyingObject() as DirectoryEntry;

                result.Add(de.Properties["givenName"].Value.CString());

                //Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
                //Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
                //Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
                //Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
                //Console.WriteLine();
            }
        }
    }
}
catch (Exception ex)
{
    return null;
}

0 个答案:

没有答案