如何获得“公司"和“部门"从 Active Directory 给定 UserPrincipal 对

How to get quot;Companyquot; and quot;Departmentquot; from Active Directory given a UserPrincipal object?(如何获得“公司和“部门从 Active Directory 给定 UserPrincipal 对象?)
本文介绍了如何获得“公司"和“部门"从 Active Directory 给定 UserPrincipal 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

Is this possible? Code sample would be nice.

推荐答案

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

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.

这里如何使用扩展方法:>

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");
        }

    }
}

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

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.

您可以通过将扩展类"添加到您的项目中来使用它,然后您可以这样做:

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"));

(顺便说一句;这对扩展属性非常有用 - 太糟糕了,它也不会出现在 C# 4 中.)

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

这篇关于如何获得“公司"和“部门"从 Active Directory 给定 UserPrincipal 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
How to register System.DirectoryServices for use in SQL CLR User Functions?(如何注册 System.DirectoryServices 以在 SQL CLR 用户函数中使用?)
Query From LDAP for User Groups(从 LDAP 查询用户组)