【问题标题】:How do I get Active Directory group id for authorized user如何获取授权用户的 Active Directory 组 ID
【发布时间】:2013-03-01 12:58:49
【问题描述】:

我的 Web 应用程序使用 Authorize 属性并指定角色来限制对某些页面的访问:

[Authorize(Roles = "AD_group1, AD_group2")]

问题是 - 有什么方法可以让授权用户(无论是 int 还是 string)获得某种 Active Directory groupId

更新: 基本思想是在数据库中存储一些表,其中包含每个组应该分开的模板。例如group1 的用户可以有一些模板来快速回答典型问题,而group2 没有这些模板,或者有一些其他模板

【问题讨论】:

    标签: asp.net-mvc c#-4.0 active-directory


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
       // find a user
       UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
       // or if you want the currently logged in user - you can also use:
       // UserPrincipal user = UserPrincipal.Current;
    
       if(user != null)
       {
           // get all groups the user is a member of
           foreach(GroupPrincipal group in user.GetAuthorizationGroups())
           {
               string distinguishedName = group.DistinguishedName;
               Guid groupGuid = group.Guid;
           }
       }
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

    • 谢谢!这就是我需要的
    • 我在生产 System.InvalidCastException 上有错误:无法将“System.DirectoryServices.AccountManagement.GroupPrincipal”类型的对象转换为“System.DirectoryServices.AccountManagement.UserPrincipal”类型?知道该怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多