简体   繁体   English

如何使用System.DirectoryServices.AccountManagement命名空间获取Active Directory用户属性?

[英]How I get Active Directory User Properties with System.DirectoryServices.AccountManagement Namespace?

I want do get Active Directory Properties from a user and I want to use System.DirectoryServices.AccountManagement . 我想从用户获取Active Directory属性,我想使用System.DirectoryServices.AccountManagement

my code: 我的代码:

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?
        }

You can transition into the DirectoryServices namespace to get any property you need. 您可以转换到DirectoryServices命名空间以获取所需的任何属性。

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

If you want to change the proppertie dont forget to call userPrincipal.save() after you changed the value. 如果你想更改属性,请不要忘记在更改值后调用userPrincipal.save()。

entry.Properties["company"].value = company;
userPrincipal.save();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Active Directory-迁移到System.DirectoryServices.AccountManagement - C# Active Directory - Migrate to System.DirectoryServices.AccountManagement 使用System.DirectoryServices.AccountManagement时,Active Directory用户创建延迟 - Delay in Active Directory user creation when using System.DirectoryServices.AccountManagement 在Windows 2000之后使用System.DirectoryServices.AccountManagement设置Active Directory用户登录名 - Setting Active Directory user login name post Windows 2000 with System.DirectoryServices.AccountManagement 如何使用System.DirectoryServices.AccountManagement将用户添加到AD? - How do I add a user to AD using System.DirectoryServices.AccountManagement? Active Directory使用System.DirectoryServices.AccountManagement在c#中搜索与特定名称匹配的所有组和用户。 - Active directory search for all groups and users matching a particular name in c# using System.DirectoryServices.AccountManagement? 如何使用 System.DirectoryServices.AccountManagement 在多个域中搜索? - How to search in multiple domains using System.DirectoryServices.AccountManagement? 我应该缓存System.DirectoryServices.AccountManagement查询的结果多少? - How much should I cache results from System.DirectoryServices.AccountManagement queries? 如何使用C#的System.DirectoryServices.AccountManagement确定OU是否存在? - How do I determine if an OU exists using C#'s System.DirectoryServices.AccountManagement? System.DirectoryServices.AccountManagement自我引导 - System.DirectoryServices.AccountManagement bootstrapping myself 使用System.DirectoryServices.AccountManagement时的DirectoryServicesCOMException - DirectoryServicesCOMException when working with System.DirectoryServices.AccountManagement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM