【问题标题】:401 and 403 Custom Error page not displaying when App deployed to Azure将应用程序部署到 Azure 时不显示 401 和 403 自定义错误页面
【发布时间】:2020-02-26 12:25:40
【问题描述】:

我有一个 ASP.NET Core WebApp。应用程序中的 AuthN 和 AuthZ 由 AAD 处理 - 如果用户尝试访问站点中的页面并且不在正确的 AAD 组中,我将尝试使用中间件并呈现 NotAuthorized 页面。

在 Configure 方法的 Startup 类中,我有以下代码:

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"), appBuilder =>
            {
                appBuilder.UseStatusCodePagesWithReExecute("/Error/{0}");
                appBuilder.UseExceptionHandler("/Error");
            });

            app.UseHsts();
        }

对于本地 IIS 测试,我注释掉了 if 块并部署到我的本地 IIS,并且在我的 AppSettings 中只添加了一个我知道我不是成员的 AAD 组 - 当我在本地点击 URL 时,我得到以下内容按我的预期工作:

我的ErrorController中的代码如下:

public class ErrorController : Controller
{
    [Route("error/404")]
    public IActionResult ErrorNotFound()
    {
        return View();
    }

    [Route("error/401")]
    [Route("error/403")]
    public IActionResult ErrorNotAuthorized()
    {
        return View();
    }

    [AllowAnonymous]

    public IActionResult Index()
    {
        return View("Index", Activity.Current?.RootId);
    }
}

但是,当我将相同的代码部署到 Azure 时,当我点击相同的 URL 时会得到以下信息 -

我是否也遗漏了什么,为什么当我的应用程序部署到 Azure 时没有呈现我的客户 403 页面?

【问题讨论】:

  • 您好,如果能解决您的问题,请尝试我的方法会很高兴。祝你好运
  • 嗨@MdFaridUddinKiron - 我不确定这是否与我需要的相同 - 您正在使用中间件处理一般错误异常 - 我正在尝试使用状态代码来处理 401 和403 - 当我在本地发布到 iis 时有效 - 只是当我在 Azure 上托管时无效
  • 啊,我想如果我能帮助你。愿你的情况和我想的不一样
  • @MdFaridUddinKiron - 没有问题 - 感谢您的努力
  • @MdFaridUddinKiron - 我认为它类似于这个答案 - 中间件未检测到 403,但作为 302 - 只是不确定如何在我的应用程序中获得相同的行为 - stackoverflow.com/questions/56868673/…

标签: c# azure asp.net-core .net-core azure-web-app-service


【解决方案1】:

我用这个答案解决了这个问题 -

ASP.net-core 3.0 - Is it possible to return custom error page when user is not in a policy?

基本上 401 或 403 不是中间件处理的所以写了我自己的自定义

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2019-06-09
    • 2018-12-20
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多