【问题标题】:How I get Active Directory User Properties with System.DirectoryServices.AccountManagement Namespace?如何使用 System.DirectoryServices.AccountManagement 命名空间获取 Active Directory 用户属性?
【发布时间】:2013-01-11 12:31:36
【问题描述】:

我想从用户那里获取 Active Directory 属性并且我想使用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?
        }

【问题讨论】:

标签: c# properties active-directory principal userprincipal


【解决方案1】:

您可以转换到 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();
    }
}

【讨论】:

    【解决方案2】:

    如果您想更改属性,请不要忘记在更改值后调用 userPrincipal.save()。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多