c# - 如何从 AD DirectoryEntry 获取 DOMAIN\USER?

标签 c# .net active-directory

如何从 Active Directory DirectoryEntry (SchemaClassName="user") 对象获取 Windows 用户和域?

用户名在 sAMAccountName 属性中,但我可以在哪里查找域名?

(我不能假设一个固定的域名,因为用户来自不同的子域。)

最佳答案

这假定 results 是从 DirectorySearcher 获得的 SearchResultCollection,但您应该能够直接从 DirectoryEntry 获得 objectsid。

SearchResult result = results[0];
var propertyValues = result.Properties["objectsid"];
var objectsid = (byte[])propertyValues[0];

var sid = new SecurityIdentifier(objectsid, 0);

var account = sid.Translate(typeof(NTAccount));
account.ToString(); // This give the DOMAIN\User format for the account

关于c# - 如何从 AD DirectoryEntry 获取 DOMAIN\USER?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/941002/

相关文章:

c# - 将 null 分配给可为空的 int 时出错 - "The value ' null' 对属性无效”

c# - 基于 DateTime 属性 C#、XPath 对 XML 节点进行排序

c# - MS Bot Framework V3 网络聊天/直线问题 - 内部服务器错误 500

.net - 如何在PowerShell中添加事件 Action 处理程序

.net - 无法加载 Exchange powershell 管理单元 : The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception

winapi - 是否有用于选择 Active Directory 用户的通用对话框?

c# - 设置 SSL 与使用 DLL 进行身份验证登录

c# - Unity 可编程对象根据另一个字段值将字段类型更改为新类型

windows - 使用 Powershell 在 Active Directory 中更新 Active Directory 用户属性

C# 字符串中字符的第三个索引