English 中文(简体)
当来电者不在域中时,请求AD中用户的角色。
原标题:
  • 时间:2009-02-04 13:39:57
  •  标签:

我想要获取ActiveDirectory中用户的组成员身份,但不在域内。当我在域内运行时,一切都正常。

var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, IdentityType.Name, "administrator");

foreach (var authorizationGroup in principal.GetAuthorizationGroups())
{
    Console.WriteLine(authorizationGroup.Name);
}

然而,当我在域外运行时,我必须像这样指定PrincipalContext:

var context = new PrincipalContext(ContextType.Domain, "10.0.1.255", "DC=test,DC=ad,DC=be", "administrator", "password");

当我运行这段代码时,我执行 principal.GetAuthorizationGroups() 时会遇到异常。我获取的异常是:

System.DirectoryServices.AccountManagement.PrincipalOperationException: Information about the domain could not be retrieved (1355).
at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags)
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName()
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p)
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper()
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups()
问题回答

看起来像是DNS问题。

DC定位器的工作方式是通过对SRV记录进行DNS查询,以找到您当前站点中适当的DC。如果那些信息不在DNS中,DC定位器将失败,这就是在您的堆栈跟踪中发生的情况。

我刚刚不得不处理同样的问题。我希望这能帮助其他人。

/*Argument*/
string username;



/*Global settings*/
string ADHost = "dc.a.b.c"; /*Or ip address*/
string ADUsername = "username";
string ADPassword = "password";
string ADDomain = "a.b.c";
string ADContainer = "DC=A,DC=B,DC=C"; /*I have a function to do the translation*/
/*Global settings*/

var list = new List<string>();

var path = "LDAP://" + ADHost + "/" + ADContainer;
var deDomain = new DirectoryEntry(path, ADUsername, ADPassword);
var ds = new DirectorySearcher(deDomain, "(&(objectClass=User)(sAMAccountName=" + username + "))");

ds.SearchScope = SearchScope.Subtree; /*Cascade*/
ds.ReferralChasing = ReferralChasingOption.All; /*Follow redirection*/

var usr = ds.FindOne();
if (null != usr)
{
    var deUsr = new DirectoryEntry(usr.Path, ADUsername, ADPassword);

    foreach (string groupDN in deUsr.Properties["memberOf"])
    {
        string[] parts = groupDN.Replace("CN=", "").Split( , );
        list.Add(parts[0]);
    }
}

现在我无法验证,可能是这样。

我尝试了以下操作:我使用了Sysinternals优秀的Active Directory Explorer。在使用相同的凭据登录时:10.0.1.255,“administrator”,“password”

现在我可以轻松查看用户的群组。

["memberOf"] = "CN=TestGroup,CN=Users,DC=test,DC=ad,DC=be"




相关问题