久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C# - 在活動目錄中查找用戶管理器

C# - Look up a users manager in active directory(C# - 在活動目錄中查找用戶管理器)
本文介紹了C# - 在活動目錄中查找用戶管理器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

開始使用 System.DirectoryServices.AccountManagement 命名空間,對活動目錄 (AD) 中的用戶執行查找.我還需要用戶的管理器,但我似乎在使用這個命名空間時遇到了障礙.獲取一個人的當前代碼:

Started using the System.DirectoryServices.AccountManagement namespace, to perform the lookup on a user in active directory (AD). I also need the user's manager, but I seem to have hit a bump in the road using this namespace. Current code to get a person:

class Person {
    // Fields
    public string GivenName = null;
    public string Surname = null;
    public string DistinguishedName = null;
    public string Email = null;
    public string MangerDistinguishedName = null;  // Unable to set this

    // Constructor
    public Person(string userName) {
        UserPrincipal user = null;

        try {
            user = GetUser(userName);

            if (user != null) {
                this.GivenName = user.GivenName;
                this.Surname = user.Surname;
                this.DistinguishedName = user.DistinguishedName;
                this.Email = user.EmailAddress;
                this.MangerDistinguishedName = user.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME>
            }
            else {
                throw new MissingPersonException("Person not found");
            }
        }
        catch (MissingPersonException ex) {
            MessageBox.Show(
                ex.Message
                , ex.reason
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error
            );
        }
        catch (Exception ex) {
            MessageBox.Show(
                ex.Message
                , "Error: Possible connection failure, or permissions failure to search for the username provided."
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error
            );
        }
        finally {
            user.Dispose();
        }
    }

執行人物搜索

    private UserPrincipal GetUser(string userName) {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);

        return user;
    }

還有什么方法可以直接訪問特定用戶的經理的專有名稱?

  • 可能的部分答案此處VB,但我不認為提及經理.
  • 另一個可能的部分問題這里,同樣,與經理無關.
  • Possible partial answer here in VB, but I see nothing about referring to managers.
  • Another possible partial one here, again, nothing about managers.

推薦答案

如果您使用 .NET 3.5 及更高版本并使用 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空間,您可以輕松擴展現有的 UserPrincipal 類以獲得更高級的屬性,例如 Manager 等.

If you're on .NET 3.5 and up and using the System.DirectoryServices.AccountManagement (S.DS.AM) namespace, you can easily extend the existing UserPrincipal class to get at more advanced properties, like Manager etc.

在此處閱讀所有相關信息:

Read all about it here:

  • 在 .NET Framework 3.5 中管理目錄安全主體莉>
  • 有關 System.DirectoryServices.AccountManagement 的 MSDN 文檔

基本上,您只需定義一個基于 UserPrincipal 的派生類,然后定義您想要的其他屬性:

Basically, you just define a derived class based on UserPrincipal, and then you define your additional properties you want:

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled) : base(context, samAccountName, password, enabled)
    {} 

    // Create the "Department" property.    
    [DirectoryProperty("department")]
    public string Department
    {
        get
        {
            if (ExtensionGet("department").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("department")[0];
        }
        set { ExtensionSet("department", value); }
    }

    // Create the "Manager" property.    
    [DirectoryProperty("manager")]
    public string Manager
    {
        get
        {
            if (ExtensionGet("manager").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("manager")[0];
        }
        set { ExtensionSet("manager", value); }
    }

    // Implement the overloaded search method FindByIdentity.
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
    }
}

現在,您可以在代碼中使用 UserPrincipalEx 的擴展"版本:

Now, you can use the "extended" version of the UserPrincipalEx in your code:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

    // you can easily access the Manager or Department now
    string department = inetPerson.Department;
    string manager = inetPerson.Manager;
}        

這篇關于C# - 在活動目錄中查找用戶管理器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 日韩不卡在线观看 | 国产一级在线观看 | 99re国产精品 | 久久久久www | 欧美三级成人理伦 | 久久久91精品国产一区二区三区 | 欧美一级片 | 国产精品亚洲综合 | 亚洲视频中文 | 欧美精品在线一区二区三区 | 中文字幕 亚洲一区 | 成人在线中文字幕 | 99久久久无码国产精品 | 国产剧情一区 | 亚洲在线中文字幕 | 欧美伊人影院 | 亚洲不卡一 | 亚洲一区二区三区在线免费 | 天堂资源 | av天天操| 欧美国产精品一区二区三区 | 国产美女在线观看 | 精品1区2区3区4区 | 精品美女久久久久久免费 | 91手机精品视频 | 精品欧美激情在线观看 | 免费黄色特级片 | 成人网址在线观看 | 日韩三级在线观看 | 久久精品久久久久久 | 亚洲一区在线观看视频 | 粉嫩高清一区二区三区 | 国产精品亚洲精品 | 日韩在线中文字幕 | 日韩欧美国产一区二区 | 国产精品揄拍一区二区 | 国际精品鲁一鲁一区二区小说 | 国内精品在线视频 | 亚洲精品乱码8久久久久久日本 | 国产精品99久久免费观看 | 欧美国产精品一区二区 |