![](/img/trans.png)
[英]Azure Active Directory retrieving same credentials for all users
[英]Retrieving custom Active Directory properties of users
我一直在尝试检索在我们的Active Directory用户上设置的两个自定义属性,但是似乎我一直在获得一个不变的属性列表(在2个不同的AD服务器上进行了测试)
假设我要获取的属性是prop1
和prop2
,以下代码中我在做什么错:
List<String> nProps = new List<string>();
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
foreach (DirectoryEntry child in directoryEntry.Children)
{
// No filtering; ignore schemes that are not User schemes
if (child.SchemaClassName == "User")
{
foreach (var sVar in child.Properties.PropertyNames)
nProps.Add(sVar.ToString());
break;
}
}
nProps
不包含任何我的自定义属性(不是prop1
或prop2
)
(它确实包含其他属性,例如BadPasswordAttempts,用户名等)
有任何想法吗?
您确定设置了属性吗? 如果未设置,则不会将其列为属性。
如果您要查找特定的属性,建议您使用DirectorySearcher
下面的示例获取给定用户的公司属性。 请注意,先验证该属性是否存在,然后再提取它。
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
//Create a searcher on your DirectoryEntry
DirectorySearcher adSearch= new DirectorySearcher(directoryEntry);
adSearch.SearchScope = SearchScope.Subtree; //Look into all subtree during the search
adSearch.Filter = "(&(ObjectClass=user)(sAMAccountName="+ username +"))"; //Filter information, here i'm looking at a user with given username
SearchResult sResul = adSearch.FindOne(); //username is unique, so I want to find only one
if (sResult.Properties.Contains("company")) //Let's say I want the company name (any property here)
{
string companyName = sResult.Properties["company"][0].ToString(); //Get the property info
}
尽管这不是您问题的直接答案,但以下是我们使用的方法:
public static string GetProperty(string adUserId, string domain, string lDAPLoginId, string lDAPPassword, string propertyName)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, lDAPLoginId, lDAPPassword);
UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, adUserId);
string result = "";
if (up != null)
{
result = PrincipalGetProperty(up, propertyName);
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.