gpt4 book ai didi

c# - 如何从 Active Directory 检索 SAMAccountName

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

我实现了一个返回 Active Directory 用户列表的方法,我想像这样检索 SAMAccountName Domain\Administrator

这是我使用的方法:

public Collection<software_user> GetUsersFromAD(String adConnectionString)
{
var users = new Collection<software_user>();

using (var directoryEntry = new DirectoryEntry(adConnectionString))
{
var directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = "(&(objectClass=user))";
var propertiesToLoad = new[]
{
"SAMAccountName",
"displayName",
"givenName",
"sn",
"mail",
"userAccountControl",
"objectSid"
};
directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad);

foreach (SearchResult searchEntry in directorySearcher.FindAll())
{
var userEntry = searchEntry.GetDirectoryEntry();
var ldapUser = new software_user();
ldapUser.User_name = NullHandler.GetString(userEntry.Properties["displayName"].Value);

if (string.IsNullOrEmpty(ldapUser.User_name))
continue;
ldapUser.User_name = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value);
ldapUser.email = NullHandler.GetString(userEntry.Properties["mail"].Value);
ldapUser.user_shortname = NullHandler.GetString(userEntry.Properties["givenName"].Value);
var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value;
//ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE;
var sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value;
//ldapUser.SId = sid;
users.Add(ldapUser);
}
}
return users;
}

最佳答案

首先:Domain\Administrator 不是 SAM 帐户名! SAM 帐户名称是唯一的(在整个域中)长度最多为 20 个字符的名称 - 通常是您的“Windows 用户名”(例如 Administrator) - 但它NOT 包含域名。由 domain\username 组成的值 NOT 存储在 Active Directory 的任何地方!


如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
// do something here....
string samAccountName = user.SamAccountName;
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩真的很容易!

如果您想搜索整个用户组(或组或计算机),您可以使用 PrincipalSearcher 和“query-by-example”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Surname = "Miller";

// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}

关于c# - 如何从 Active Directory 检索 SAMAccountName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112062/

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