如何在ASP.NET Core中返回401而不是302? [英] How to return 401 instead of 302 in ASP.NET Core?
本文介绍了如何在ASP.NET Core中返回401而不是302?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取ASP.NET Core身份以在用户未登录时返回401.我向方法中添加了[Authorize]
属性,而不是返回401,而是返回302.尝试了很多建议,但似乎无济于事,包括services.Configure
和app.UseCookieAuthentication
将LoginPath
设置为null
或PathString.Empty
.
I'm trying to get ASP.NET Core Identity to return 401 when a user isn't logged in. I've added an [Authorize]
attribute to my method and instead of returning 401 it returns 302. I've tried a ton of suggestions but nothing seems to work, including services.Configure
and app.UseCookieAuthentication
setting LoginPath
to null
or PathString.Empty
.
推荐答案
从 ASP.NET Core 2.x 开始:
services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToLogin = context =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
这篇关于如何在ASP.NET Core中返回401而不是302?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文