提问人:bilpor 提问时间:11/21/2018 最后编辑:bilpor 更新时间:11/21/2018 访问量:1929
中间件未在 .net core 2.1 中预期时调用
Middleware not invoking when expected in .net core 2.1
问:
在我的配置方法的启动.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}");
}
然后在授权过滤器中,我有一些代码,在某些情况下我有:
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;
}
当我使用谷歌开发人员工具时,我可以看到 401 被正确返回,但我上面的管道永远不会被命中。我需要如何更改此设置,以便每当从应用中的任何位置引发 401 时,都会发生重定向?
答: 暂无答案
评论
.Use(...)
.UseAuthentication(...)
IAuthorizationFilter
Use(...)