如果Asp.net Core 3.1中的Authorize Attribute失败,如何拒绝更改访问?重定向自定义错误页面

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

我使用TransformAsync(ClaimsPrincipal主体)方法来添加角色

  var c = new Claim(currentUser.RoleClaimType, "Admin");
                currentUser.AddClaim(c);

在我的控制器中:

[Authorize(Roles = "Admin")]

但是如果使用则没有管理员角色=> acces denied to this page 403

我的问题是:

如果授权失败,如何重定向到自定义错误页面?

asp.net redirect core
1个回答
0
投票

如果其他人有相同的问题,请解决:我在我的Statup.cs中添加此代码在configure方法中:

app.UseStatusCodePages(异步上下文=> {var response = context.HttpContext.Response;

            if (response.StatusCode == (int)HttpStatusCode.Forbidden)
            {
                response.Redirect("/Home/Error");
            }

        });
© www.soinside.com 2019 - 2024. All rights reserved.