如何使用System.DirectoryServices.AccountManagement命名空间获取Active Directory用户属性? [英] How I get Active Directory User Properties with System.DirectoryServices.AccountManagement Namespace?

查看:138
本文介绍了如何使用System.DirectoryServices.AccountManagement命名空间获取Active Directory用户属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户那里获取Active Directory属性,并且想使用 System.DirectoryServices.AccountManagement

I want do get Active Directory Properties from a user and I want to use System.DirectoryServices.AccountManagement.

我的代码:

public static void GetUserProperties(string dc,string user) 
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
            UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);

            string firstname = u.GivenName;
            string lastname = u.Surname;
            string email = u.EmailAddress;
            string telephone = u.VoiceTelephoneNumber;

            ...//how I can get company and other properties?
        }


推荐答案

您可以过渡到DirectoryServices

You can transition into the DirectoryServices namespace to get any property you need.

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);

string firstname = u.GivenName;
string lastname = u.Surname;
string email = u.EmailAddress;
string telephone = u.VoiceTelephoneNumber;
string company = String.Empty;

...//how I can get company and other properties?
if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
    // Transition to directory entry to get other properties
    using (var entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject())
    {
        if (entry.Properties["company"] != null)
            company = entry.Properties["company"].Value.ToString();
    }
}

这篇关于如何使用System.DirectoryServices.AccountManagement命名空间获取Active Directory用户属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆