简体   繁体   English

如何检索用户所属的所有角色(组)?

[英]How can I retrieve all the roles (groups) a user is a member of?

有没有办法获取Windows身份验证用户所在的角色列表,而无需通过WindowsPrincipal.IsInRole方法进行显式检查?

WindowsPrincipal.IsInRole just checks if the user is a member of the group with that name; WindowsPrincipal.IsInRole只检查用户是否是具有该名称的组的成员; a Windows Group is a Role. Windows组是一个角色。 You can get a list of the groups that a user is a member of from the WindowsIdentity.Groups property. 您可以从WindowsIdentity.Groups属性中获取用户所属组的列表。

You can get WindowsIdentity from your WindowsPrincipal : 您可以从WindowsPrincipal获取WindowsIdentity

WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity;

or you can get it from a factory method on WindowsIdentity: 或者您可以从WindowsIdentity上的工厂方法获取它:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

WindowsIdenity.Groups is a collection of IdentityReference which just gives you the SID of the group. WindowsIdenity.GroupsIdentityReference的集合, IdentityReference为您提供组的SID。 If you need the group names you will need to translate the IdentityReference into an NTAccount and get the Value: 如果您需要组名,则需要将IdentityReference转换为NTAccount并获取值:

var groupNames = from id in identity.Groups
                 select id.Translate(typeof(NTAccount)).Value;

EDIT: Josh beat me to it! 编辑:乔希打败了我! :) :)

Try this 尝试这个

using System;
using System.Security.Principal;

namespace ConsoleApplication5
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var identity = WindowsIdentity.GetCurrent();

            foreach (var groupId in identity.Groups)
            {
                var group = groupId.Translate(typeof (NTAccount));
                Console.WriteLine(group);
            }
        }
    }
}

If you are not connected to the domain server, the Translate function may throw the following exception The trust relationship between this workstation and the primary domain failed. 如果未连接到域服务器,则Translate功能可能会引发以下异常The trust relationship between this workstation and the primary domain failed.

But for most of the groups, it will be OK, so I use: 但是对于大多数人来说,它会没问题,所以我使用:

foreach(var s in WindowsIdentity.GetCurrent().Groups) {
    try {
        IdentityReference grp = s.Translate(typeof (NTAccount)); 
        groups.Add(grp.Value);
    }
    catch(Exception) {  }
}

In an ASP.NET MVC site, you can do it like this: 在ASP.NET MVC站点中,您可以这样做:

Add this to your Web.config: 将其添加到您的Web.config:

<system.web>
  ...
  <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
  ...
</system.web>

Then you can use Roles.GetRolesForUser() to get all the Windows groups that the user is a member of. 然后,您可以使用Roles.GetRolesForUser()来获取该用户所属的所有Windows组。 Make sure you're using System.Web.Security . 确保您using System.Web.Security

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得AD用户的组成员身份-包括其他域中的所有组? - How can I get the group memberships for an AD user - including all groups in other domains? 如何检索事件的所有方法? - How can I retrieve all methods of an event? Specflow:如何创建一个共享步骤来规则所有登录到具有多个用户角色的系统? - Specflow : How do I create a single shared step to rule all Logins to a system with multiple user roles? 如何获取特定用户的所有 AD 组? - How to get all the AD groups for a particular user? 如何使用SSRS和BIDS自定义安全扩展访问用户角色/权限? - How can I access user roles/permissions with SSRS and BIDS Custom Security Extension? 如何检查用户是否属于具有 MVC4 简单成员资格的几种不同角色中的任何一种? - How can I check if a user is in any one of a few different roles with MVC4 Simple membership? 如何检索XmlDocument对象中的所有XmlEntityReference对象? - How Can I Retrieve All XmlEntityReference Objects In An XmlDocument Object? 如何检索Dictonary中包含的所有KeyValuePairs <?,?> ? - How can I retrieve all the KeyValuePairs contained in a Dictonary<?,?>? 如何检索类中的所有自定义属性 - How can I retrieve all custom attributes on a class 如何使用js获取用户成员所属的所有团队(TFS 2015扩展)? - How do I get all the teams that the user member is (TFS 2015 extension) using js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM