- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经根据教程实现了 JWT 身份验证/授权系统。
但不幸的是我不知道如何授权这些角色。
这是基于角色的授权。
public class InfoController : ControllerBase
{
[Authorize(Roles = "User")]
[HttpGet("user")]
public IActionResult UserInfo()
{
return Ok("User role");
}
}
当我调用此 api/Info/user
时,我得到 401 响应代码而不是具有“用户角色”的状态代码 200信息。所以基于角色的授权不起作用。我用正确的数据调用它:
curl -X GET "https://localhost:34545/api/Info/user" -H "accept: application/json" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwianRpIjoiYTgwYzQ3NDQtNmYyZC00ZTAwLThhYjItMzY2MTRmYTNmMzYzIiwiaWF0IjoxNTU3NjgyMDU4LCJyb2wiOiJ3ZWItYXBpLWFjYyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IlVzZXIiLCJuYmYiOjE1NTc2ODIwNTgsImV4cCI6MTU1NzY4OTI1OCwiaXNzIjoid2ViLWFwaSIsImF1ZCI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzUwLyJ9.8uRAOsSXmKeWcYCwvN0sNFX02GNoZMaPNf6RPGkQE4E"
在启动时我有以下配置:
services.AddAuthorization(options =>
{
options.AddPolicy("api", policy => policy.RequireClaim(
jwtClaimConfiguration[nameof(JwtClaimOptions.Rol)],
jwtClaimConfiguration[nameof(JwtClaimOptions.ApiAccess)]
));
options.AddPolicy("user", policy => policy.RequireRole(AppRole.USER));
options.AddPolicy("admin", policy => policy.RequireRole(AppRole.ADMIN));
});
这里是生成Jwt的地方(我在这里看到过StackOverflow这个解决方案)
public async Task<string> GenerateEncodedTokenAsync(string userName, ClaimsIdentity claimsIdentity)
{
List<Claim> claims = new List<Claim>()
{
new Claim(JwtRegisteredClaimNames.Sub, userName),
new Claim(JwtRegisteredClaimNames.Jti, await jwtOptions.JtiGenerator()),
new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
claimsIdentity.FindFirst(jwtClaimConfiguration[nameof(JwtClaimOptions.Rol)]),
claimsIdentity.FindFirst(jwtClaimConfiguration[nameof(JwtClaimOptions.ApiAccess)])
};
var user = await userManager.FindByNameAsync(userName);
var roles = await userManager.GetRolesAsync(user);
claims.AddRange(roles.Select(role => new Claim(ClaimsIdentity.DefaultRoleClaimType, role)));
JwtSecurityToken jwt = new JwtSecurityToken(
issuer: jwtOptions.Issuer,
audience: jwtOptions.Audience,
notBefore: jwtOptions.NotBefore,
expires: jwtOptions.Expiration,
signingCredentials: jwtOptions.SigningCredentials,
claims: claims
);
return new JwtSecurityTokenHandler().WriteToken(jwt);
}
Jwt token 包含角色!这是有效载荷,如您所见,已添加角色,用户具有“用户”角色。:
{
"sub": "demo",
"jti": "a80c4744-6f2d-4e00-8ab2-36614fa3f363",
"iat": 1557682058,
"rol": "acc",
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "User",
"nbf": 1557682058,
"exp": 1557689258,
"iss": "api",
"aud": "https://localhost:34545/"
}
最佳答案
请正确订购中间件
first => app.UseRouting();
second => app.UseAuthentication();
third=> app.UseAuthorization();
关于c# - ASP.NET Core WebApi Jwt 基于角色的授权不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101873/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!