检索用户的自定义 Active Directory 属性
- 作者: 河北高学成
- 来源: 51数据库
- 2022-10-21
问题描述
我一直在尝试检索为我们的 Active Directory 用户设置的两个自定义属性,但似乎我一直在获取一个恒定的属性列表(在 2 个不同的 AD 服务器上测试)
I've been trying to retrieve two custom properties that are set on our Active Directory users, but it seems like I keep getting a constant list of Properties (tested over 2 different AD servers)
假设我试图获取的属性是 prop1 和 prop2,我在下面的代码中做错了什么:
Assuming the properties I'm trying to fetch are prop1 and prop2, What am I doing wrong in the following code:
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)
nProps does not contain ANY of my custom properties (not prop1 nor prop2)
(它确实包含其他属性,例如 BadPasswordAttempts、用户名等)
(it does contain other properties, like BadPasswordAttempts, Username, etc)
有什么想法吗?
推荐答案
你确定你的属性设置了吗?如果未设置它们,它们将不会被列为属性.
如果您正在寻找特定的属性,我建议您使用 目录搜索器
以下示例获取给定用户的公司属性.请注意,您首先验证该属性是否存在,然后提取它.
Are you sure your properties are set? if they are not set, they are not gonna be listed as properties.
If you're looking for specific properties, I'd recommend you to use DirectorySearcher
The following example is getting the company property of a given user. Note that you verify if the property exists first, then you extract it.
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
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
