中间件在 .net core 2.1 中未按预期调用

bil*_*por 5 c# asp.net-core-mvc asp.net-core

在我的配置方法的startup.cs文件中,我有以下内容:

public void Configure(IApplicationBuilder app)
    {
        if (HostingEnvironment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            //TODO: Create an exception handler controller...
            app.UseExceptionHandler("/Home/Error");
        }

     //   app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent()); //required to force URL to https

        app.UseStaticFiles();
        app.UseSession();

        app.UsePartialPageRendering();

        app.Use(async (context, next) =>
        {
            await next.Invoke();
            if (context.Response.StatusCode == 401)
            {
                context.Response.Redirect("/user/sign-in");
            }
        });


        app.UseMvc();
        // Log the environment so we can tell if it is correct.
        Logger.LogInformation($"Hosting environment: {HostingEnvironment.EnvironmentName}");
    }
Run Code Online (Sandbox Code Playgroud)

然后在授权过滤器中我有一些代码,在某些情况下我有:

var authorisationCookie = _httpContext.HttpContext.Request.Cookies.Where(t => t.Key == "aut").FirstOrDefault();
        //no valid cookie
        if (authorisationCookie.Key == null)
        {
            //Kill the pipeline so that view doesn't get displayed.
            context.Result = new UnauthorizedResult(); //should be this, but trouble handling it client side.

            return;
        }
Run Code Online (Sandbox Code Playgroud)

如果我使用谷歌开发者工具,当这条线被击中时,我可以看到 401 被正确返回,但我上面的管道永远不会被击中。我需要如何更改此设置,以便每当从应用程序中的任何位置引发 401 时都会发生重定向?