我使用TransformAsync(ClaimsPrincipal主体)方法来添加角色
var c = new Claim(currentUser.RoleClaimType, "Admin");
currentUser.AddClaim(c);
在我的控制器中:
[Authorize(Roles = "Admin")]
但是如果使用则没有管理员角色=> acces denied to this page 403
我的问题是:
如果授权失败,如何重定向到自定义错误页面?
如果其他人有相同的问题,请解决:我在我的Statup.cs中添加此代码在configure方法中:
app.UseStatusCodePages(异步上下文=> {var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Forbidden)
{
response.Redirect("/Home/Error");
}
});