如何获取 Active Directory 中用户的组?

发布于 2024-12-09 02:54:17 字数 803 浏览 0 评论 0原文

我从这段代码中使用:

   List<GroupPrincipal> result = new List<GroupPrincipal>();

   // establish domain context
   PrincipalContext MyDomain = new PrincipalContext(ContextType.Domain);

   // find your user
   UserPrincipal user = UserPrincipal.FindByIdentity(MyDomain , username);

   // if found - grab its groups
   if(user != null)
   {
      PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();

      // iterate over all groups
      foreach(Principal p in groups)
      {
         // make sure to add only group principals
         if(p is GroupPrincipal)
         {
             result.Add(p);
         }
      }
   }

但是在这一行( user.GetAuthorizationGroups() )我得到了一个异常

该服务器无法运行

I use from this code:

   List<GroupPrincipal> result = new List<GroupPrincipal>();

   // establish domain context
   PrincipalContext MyDomain = new PrincipalContext(ContextType.Domain);

   // find your user
   UserPrincipal user = UserPrincipal.FindByIdentity(MyDomain , username);

   // if found - grab its groups
   if(user != null)
   {
      PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();

      // iterate over all groups
      foreach(Principal p in groups)
      {
         // make sure to add only group principals
         if(p is GroupPrincipal)
         {
             result.Add(p);
         }
      }
   }

but on this line ( user.GetAuthorizationGroups() ) I got an exception

This server is not operational

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

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

发布评论

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

评论(6

感性不性感 2024-12-16 02:54:17

在 Web 环境中:

System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups

或在您的上下文中:

user.GetGroups()

MSDN

In a web environment:

System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups

or in your context:

user.GetGroups()

MSDN

是伱的 2024-12-16 02:54:17

据我所知,异常可能是由于任何可用的 DNS 都无法解析域名而引起的。确保它是这样,异常就会消失。

From what I remember, the exception can be caused by the fact that the domain name cannot be resolved with any available DNS. Make sure that it is and the exception goes away.

jJeQQOZ5 2024-12-16 02:54:17

检查一下是否可以帮助您
http://support.microsoft.com/kb/842789

更新:

管理员身份打开 Visual Studio,然后打开您的解决方案。然后再试一次。我相信你的问题是因为申请许可。

Check this if it can help you
http://support.microsoft.com/kb/842789

Update :

Open Visual Studio As Administrator and then open your solution. then try again. I believe that your problem is because of application permition.

小巷里的女流氓 2024-12-16 02:54:17

当我从一台不属于我查询的域的计算机(开发计算机)运行我的程序之一时,我遇到了完全相同的问题。我的意思是我获得了上下文,获得了 UserPrincipal 信息,并且当我调用 GetGroups() 时遇到了相同的错误。在服务器本身上运行的相同程序可以完美运行。

我尝试设法让我的开发计算机直接配置域 DNS 作为第一个 DNS,但结果是一样的。

我尝试在主机文件中硬配置域和 DC 地址,但结果是一样的。

因此,我从域中的虚拟机远程调试我的程序。

I've got exactly the same trouble when I run one of my program from a computer (dev computer) which does not belong to the domain I query for. I mean that I got the context, I got the UserPrincipal information and I've got the same error when I call GetGroups(). The same program run on the server itself works perfectly.

I tryed to manage to have my developpment computer directly configured with the domain DNS as first DNS, but it was the same.

I tryed to hard configure domain and DC adresses in hosts file but it was the same.

So I remote debug my program from a virtual machine which was in the domain.

如歌彻婉言 2024-12-16 02:54:17

我再次安装 Active Directory,问题得到解决...

I install my Active Directory again and my problem resolved...

阳光的暖冬 2024-12-16 02:54:17

您尝试获取的 GetGroups 中包含的组之一是管理员组,需要特殊权限。尝试在您的上下文中设置用户/密码并使用 getGroups(context)

One of the groups your trying to grab included in the GetGroups is an administrator group and requires special permission. Try setting a user/password in your context and use getGroups(context)

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