gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:59:58 25 4
gpt4 key购买 nike

我想从用户那里获取 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?
}

最佳答案

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

关于c# - 如何使用 System.DirectoryServices.AccountManagement 命名空间获取 Active Directory 用户属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com