如何在 AD 中查找属于某个组的用户,并获取他们的 SAMAccountName 和 SID?
我只希望用户能够在文本框中输入组名,并仅返回他们的登录名和 SID。
到目前为止,我已经有了这个,并且加载了组中的用户,但我不确定如何提取登录名和 SID。
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", txtGroup.Text);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("objectGUID");
result = search.FindOne();
StringBuilder userNames = new StringBuilder();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.AppendLine(user);
}
}
lblResults.Text = userNames.ToString();
I just want a user to be able to type in a group name in a textbox, and return just their login name and their SID.
So far i have this, and that loads the users in the group but im unsure how to extract the login and SID.
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", txtGroup.Text);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("objectGUID");
result = search.FindOne();
StringBuilder userNames = new StringBuilder();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.AppendLine(user);
}
}
lblResults.Text = userNames.ToString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包含 SID 的属性称为
objectSid
,包含登录名的属性为sAMAccountName
(对于 NT4 兼容版本)和userPrincipalName
。你最好听从@Vikkunen 的建议。The propertie wich contains SID is called
objectSid
, and the propertie wich contain th login issAMAccountName
for the NT4 compatible version anduserPrincipalName
. You'd better work with @Virkkunen advice.我认为如果您反转查询,效果会更好:
这样您就可以使用
FindAll
直接获取用户列表。不要忘记将sAMAccountName
等添加到PropertiesToLoad
中。I think it would work better if you reverse your query:
This way you'll directly get back a list of users by using
FindAll
. Don't forget to addsAMAccountName
etc intoPropertiesToLoad
.