在ASP.NET Core Web API中访问和使用UserClaims。

问题描述 投票:0回答:1

我是第一次使用ASP.NET Core Web API,对它很陌生。我已经添加了索赔,现在我想排序 "读取 "和访问它们。

谁能帮我解决这个问题?

c# asp.net-core asp.net-web-api claims-based-identity claims
1个回答
0
投票

其实很简单。当你创建JWT令牌时,你就可以向它添加权限。它可以是userId,他的角色等。

所以当你设置 授权与承载计划,每个需要授权的请求都会被验证。例如,这个方法将只允许使用jwt令牌和 索赔型 角色管理员 :

[Authorize(AuthenticationSchemes = "Bearer")]
public class YourController : ControllerBase
{
    ...
    [HttpGet]
    [Authorize(Roles = "admin")]
    public async Task<IActionResult> Method(string id)
    {
    }
    ...
}

如果代币没有所需的权利要求,你将得到以下信息 403 Forbidden 回应

© www.soinside.com 2019 - 2024. All rights reserved.