C# 获取用户所属的本地和域帐户组
我试图列出给定用户所属的所有组,我希望同时支持本地计算机帐户和域帐户。我可以使用以下代码获取域帐户的组:C# 获取用户所属的本地和域帐户组,c#,account-management,C#,Account Management,我试图列出给定用户所属的所有组,我希望同时支持本地计算机帐户和域帐户。我可以使用以下代码获取域帐户的组: public void ListAllGroupsDomain() { ListAllGroups(ContextType.Domain, "myDomain", "myDomainUser", "myDomainPassword"); } public void ListA
public void ListAllGroupsDomain()
{
ListAllGroups(ContextType.Domain,
"myDomain",
"myDomainUser",
"myDomainPassword");
}
public void ListAllGroups(ContextType contextType
string name,
string username,
string password)
{
using (var context = new PrincipalContext(contextType,
name,
username,
password))
{
using (var findByIdentity = UserPrincipal.FindByIdentity(context, "testedit"))
{
if (findByIdentity != null)
{
var groups = findByIdentity.GetGroups(context);
var results = groups.Select(g => g.Name).ToArray();
Console.WriteLine("Listing {0} groups", results.Count());
foreach (var name in results)
{
Console.WriteLine("{0}", name);
}
}
}
}
}
但是,如果我尝试调用计算机本地的帐户,我会收到一个COM异常,说我的用户名或密码不正确(事实并非如此):
我猜我对机器帐户使用的FindByIdentity不正确。所以我尝试了一种稍微不同的方法,不使用FindByIdentity:
public void ListAllGroups2(ContextType contextType
string name,
string username,
string password)
{
using (var context = new PrincipalContext(contextType,
name,
username,
password))
{
using (var userContext = new UserPrincipal(context))
{
var results = userContext.GetGroups();
Console.WriteLine("Listing {0} groups:", results.Count());
foreach (var principal in results)
{
Console.WriteLine("{0}", principal.Name);
}
}
}
}
这个版本没有抛出任何异常,但是对
GetGroups()
的调用也没有返回任何结果。我如何才能获得某个特定用户所属的组,这些组对计算机帐户和域帐户都是可靠的?多亏了我从中获得的一点帮助,我才知道我做错了什么。我错误地设置了PrincipalContext。我不需要给它域、用户名或密码,只需要上下文类型。以下代码适用于本地计算机帐户和域帐户:
public void ListAllGroupsDomain()
{
ListAllGroups(ContextType.Domain, "myDomainUsername");
}
public void ListAllGroupsMachine()
{
ListAllGroups(ContextType.Machine, "myMachineUsername");
}
public void ListAllGroups(ContextType contextType, string userName)
{
using (var context = new PrincipalContext(contextType))
{
using (var findByIdentity = UserPrincipal.FindByIdentity(context, userName))
{
if (findByIdentity != null)
{
var groups = findByIdentity.GetGroups(context);
var results = groups.Select(g => g.Name).ToArray();
Console.WriteLine("Listing {0} groups", results.Count());
foreach (var name in results)
{
Console.WriteLine("{0}", name);
}
}
}
}
}
我需要确保做的唯一一件事是在我将域传递给
ListAllGroups
之前从用户名中删除域,因为在某些情况下,提供给我的函数会将其前置到用户名。这正是我要寻找的atm,但是我访问.FindByIdentity和.GetGroups调用的速度非常慢。你有没有遇到过这样的性能问题?对你来说有多慢,斯派克?当我在做这个的时候,我尝试了几种不同的方法,我似乎记得有些方法几乎是瞬间的,有些方法需要10秒以上的时间。但是,时间太长了,我不能告诉你更多。FindByIdentity大约需要4秒(使用ContextType.Machine)和GetGroups大约需要6秒。不过,我不在一个域上——Windows 8.1,通过IISExpress运行。我想是时候进行缓存了。
public void ListAllGroupsDomain()
{
ListAllGroups(ContextType.Domain, "myDomainUsername");
}
public void ListAllGroupsMachine()
{
ListAllGroups(ContextType.Machine, "myMachineUsername");
}
public void ListAllGroups(ContextType contextType, string userName)
{
using (var context = new PrincipalContext(contextType))
{
using (var findByIdentity = UserPrincipal.FindByIdentity(context, userName))
{
if (findByIdentity != null)
{
var groups = findByIdentity.GetGroups(context);
var results = groups.Select(g => g.Name).ToArray();
Console.WriteLine("Listing {0} groups", results.Count());
foreach (var name in results)
{
Console.WriteLine("{0}", name);
}
}
}
}
}