C# 如何从给定 UserPrincipal 对象的 Active Directory 中获取“公司”和“部门”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1785751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:48:24  来源:igfitidea点击:

How to get "Company" and "Department" from Active Directory given a UserPrincipal object?

c#.netactive-directory

提问by wgpubs

Is this possible? Code sample would be nice.

这可能吗?代码示例会很好。

采纳答案by Per Noalt

Actually, the question was how to get two of the properties for a .NET 3.5 (System.DirectoryServices.AccountManagement.)UserPrincipal-object not given a userPrincipalName.

实际上,问题是如何为(System.DirectoryServices.AccountManagement.)UserPrincipal没有给定.NET 3.5 对象的两个属性获取userPrincipalName.

Here how to do that with an extension method:

这里如何使用扩展方法做到这一点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace MyExtensions
{
    public static class AccountManagementExtensions
    {

        public static String GetProperty(this Principal principal, String property)
        {
            DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
            if (directoryEntry.Properties.Contains(property))
                return directoryEntry.Properties[property].Value.ToString();
            else
                return String.Empty;
        }

        public static String GetCompany(this Principal principal)
        {
            return principal.GetProperty("company");
        }

        public static String GetDepartment(this Principal principal)
        {
            return principal.GetProperty("department");
        }

    }
}

The above code will work in most cases (that is it will work for standard Text/String Single-Value Active Directory attributes). You'll need to modify the code and add more error handling code for your environment.

以上代码适用于大多数情况(即适用于标准文本/字符串单值 Active Directory 属性)。您需要修改代码并为您的环境添加更多错误处理代码。

You use it by add the "Extension Class" to your project and then you can do this:

您可以通过将“扩展类”添加到您的项目来使用它,然后您可以执行以下操作:

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, "youruser");
Console.WriteLine(userPrincipal.GetCompany());
Console.WriteLine(userPrincipal.GetDepartment());
Console.WriteLine(userPrincipal.GetProperty("userAccountControl"));

(BTW; this would have been a great use for Extension Properties - too bad it won't be in C# 4 either.)

(顺便说一句;这对于扩展属性来说是一个很好的用途——太糟糕了,它也不会出现在 C# 4 中。)

回答by Mikael Svenson

Something like this should do it if the department and company properties exist for the user.

如果用户的部门和公司属性存在,则应该执行这样的操作。

DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://dnsNameOfYourDC.my.company.com";
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.PropertiesToLoad.Add("department");
deSearch.PropertiesToLoad.Add("company");

deSearch.SearchScope = SearchScope.Subtree;
deSearch.Filter = "(&(objectClass=User)(userPrincipalName=MyPrincipalName))";
SearchResultCollection results = deSearch.FindAll():

foreach (SearchResult result in results)
{
    ResultPropertyCollection props = result.Properties;
    foreach (string propName in props.PropertyNames)
    {
       //Loop properties and pick out company,department
       string tmp = (string)props[propName][0];
    }
}