当调用者不在域中时请求用户在 AD 中的角色

发布于 2024-07-13 08:07:43 字数 1347 浏览 8 评论 0原文

我想在 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()

I would like to get a user's group memberships in an ActiveDirectory, without being in the domain. When I run this inside the domain, all is well.

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

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

However, when I run outside the domain, I have to specify the PrincipalContext lie this:

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

When I run this code, I get an exception when I execute principal.GetAuthorizationGroups(). The exception I get is:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慵挽 2024-07-20 08:07:44

看起来像是 DNS 问题。

DC 定位器的工作原理是对 SRV 记录进行 DNS 查询,以在当前站点中查找适当的 DC。 如果这些东西不在 DNS 中,DC 定位器将失败,这发生在堆栈跟踪中。

Looks like a DNS problem.

DC locator works by doing DNS queries for SRV records to find the appropriate DC in your current site. If that stuff isn't in DNS, DC locator will fail, which is happening in your stack trace.

日暮斜阳 2024-07-20 08:07:44

我只需要处理同样的问题。 我希望这对其他人有帮助。

/*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]);
    }
}

I just had to deal with same problem. I hope this help someone else.

/*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]);
    }
}
爱你是孤单的心事 2024-07-20 08:07:44

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

我尝试了以下方法:我使用 sysinternals 优秀的 Active DirectoryExplorer。 使用相同凭据登录时: 10.0.1.255, "administrator", "password"

现在我可以看到用户的组没有问题,如下所示

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

It might be that, I can't verify it right now.

I tried the following: I use sysinternals' excellent Active DirectoryExplorer. When logging in with the same credentials: 10.0.1.255, "administrator", "password"

Now I can see the user's groups without problems as

["memberOf"] = "CN=TestGroup,CN=Users,DC=test,DC=ad,DC=be"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文