我正在尝试确定给定的本地用户帐户是否在本地管理员组中。一切正常,直到系统加入域。当加入域时,会抛出未找到网络路径的异常,但仅在查找本地非管理员帐户时才会抛出;如果测试帐户是本地管理员,则该方法返回正常。
这是代码的示例:
string accountName = @"localAccountName";
string groupName = @"Administrators";
using (PrincipalContext principalContext = new PrincipalContext(ContextType.Machine))
{
using (UserPrincipal accountPrinciple = new UserPrincipal(principalContext))
{
accountPrinciple.SamAccountName = accountName;
using (PrincipalSearcher accountSearcher = new PrincipalSearcher(accountPrinciple))
{
UserPrincipal account = (UserPrincipal)accountSearcher.FindOne();
if(account != null)
{
using (GroupPrincipal groupPrinciple = new GroupPrincipal(principalContext))
{
groupPrinciple.SamAccountName = groupName;
using (PrincipalSearcher groupSearcher = new PrincipalSearcher(groupPrinciple))
{
GroupPrincipal group = (GroupPrincipal)groupSearcher.FindOne();
if (account.IsMemberOf(group))
{
Console.WriteLine(@"{0} is part of the administrators group", accountName);
}
else
{
Console.WriteLine(@"{0} is not …Run Code Online (Sandbox Code Playgroud)