简体   繁体   English

如何检查用户是否在 Asp.Net Core Identity Framework 中担任角色

[英]How to check if a user is in a role in Asp.Net Core Identity Framework

I've an app that will have multiples level of organization, and for each level, there will be rights(admin-reader-...).我有一个应用程序,它将具有多个级别的组织,并且对于每个级别,都会有权限(admin-reader-...)。

I want to create(and maintain) a list of roles for each user, but it means that a lot of those roles name will be dynamic, like {id-of-the-organization]-admin .我想为每个用户创建(和维护)一个角色列表,但这意味着很多角色名称都是动态的,比如{id-of-the-organization]-admin

Therefore, I cannot just do the usual Authorize :因此,我不能只做通常的Authorize

[Authorize(Roles = "Administrator, PowerUser")]
public class ControlAllPanelController : Controller
{
    public IActionResult SetTime() =>
        Content("Administrator || PowerUser");

    [Authorize(Roles = "Administrator")]
    public IActionResult ShutDown() =>
        Content("Administrator only");
}

I would like to have something like我想要类似的东西

public class ControlAllPanelController : Controller
{
    [Authorize]
    public IActionResult SetTime(Guid organizationId) {
        someService.Authorize(organizationId+"-SetTime");//Throw exception or return boolean
        //... rest of my logic
    }
}

Not sure how to achieve this?不确定如何实现这一目标? I've seen example of this with the IAuthorize service, but this was requiring to provide policies name, which I don't have for this case(Or maybe there is one by default but I don't know its name. `我已经在IAuthorize服务中看到过这样的例子,但这需要提供策略名称,对于这种情况我没有(或者默认情况下可能有一个,但我不知道它的名称。`

I've seen that the ClaimsPrincipal has a IsInRole , but I'm not totally sure it get the latest information from Asp.Net Core Identity Framwork(from the user manager) (only what is stored inside the token?)?我已经看到ClaimsPrincipal有一个IsInRole ,但我不完全确定它从 Asp.Net Core Identity Framwork(来自用户管理器)获取最新信息(仅存储在令牌中的内容?)?

You can use HttpContext to look at the claims in the JWT.可以使用HttpContext查看JWT中的声明。

I have recently been working with authorizations in .NET API and this is what I done:我最近一直在使用 .NET API 中的授权,这就是我所做的:

var identity = this.HttpContext.User.Identities.FirstOrDefault();
var role = identity.Claims.FirstOrDefault(x => x.Type == "role").Value;
                    
if (role != "Admin")
{
    return Unauthorized("You don't have to correct permissons to do this.");
}

So I'm getting the Identity details, then searching the claims for the role claim.所以我正在获取身份详细信息,然后在声明中搜索role声明。

As a side note, Im using this in a controller inheriting from ControllerBase so I believe HttpContext is a property of this class so no need to inject it if you're using this.作为旁注,我在继承自ControllerBase的 controller 中使用它,因此我相信HttpContext是此 class 的属性,因此如果您正在使用它,则无需注入它。 Else, you'd probably have to use it via DI, but should all work the same.否则,您可能必须通过 DI 使用它,但都应该一样工作。

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

相关问题 如何检查登录用户是否在 ASP.NET Core 中扮演角色 - How to check if a logged in user is in a role in ASP.NET Core 如何在 ASP.NET Core 中以编程方式检查用户是否处于角色中? - How to check that user is in a role programmatically in ASP.NET Core? ASP.Net Core如何获得EF Core和Identity中的用户角色 - ASP.Net Core how to get the user role in EF Core and Identity 无法检查用户是否在角色ASP.NET Identity 2.0中 - Cant check if User is in a Role ASP.NET Identity 2.0 ASP.NET Identity 2.0检查当前用户是否处于角色IsInRole中 - ASP.NET Identity 2.0 check if current user is in role IsInRole ASP.NET 核心 Web API - 如何 ZE0626222614BDEE31951D84C64E5E 使用基于登录用户角色的 DBEE31951D84C64E5E 身份登录用户角色 - ASP.NET Core Web API - How to Select Records based on logged in user role using DB Identity 如何使用ASP.NET Core Identity v Preview 3.0创建自定义用户和角色? - How i can create custom user and role with ASP.NET Core identity v preview 3.0? 如何刷新当前经过身份验证的用户的角色更改了ASP.NET身份框架中的成员身份? - How to refresh current authenticated user's role changed membership in ASP.NET identity framework? 播种asp.net核心身份角色 - Seeding asp.net core Identity Role 如何在asp.net core中添加多个身份和多个角色 - How to add multiple identity and multiple role in asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM