Mat*_*kan 8 cookies asp.net-core asp.net-core-identity asp.net-core-2.0
我从ASP.NET Core 1.1升级到2.0,现在有401个未经授权的响应被更改为302重定向响应.这在以前是我在1.1中的一个问题,并通过以下代码进行了缓解:
services.AddIdentity<User, IdentityRole>(identityOptions =>
{
identityOptions.Cookies.ApplicationCookie.AutomaticChallenge = false;
})
Run Code Online (Sandbox Code Playgroud)
但是,不再Cookies有财产identityOptions.
我也试过添加以下内容(并且还注意到我以前在我的应用中不需要这种扩展方法):
services.AddCookieAuthentication(cookieAuthenticationOptions => {
cookieAuthenticationOptions.LoginPath = ""; // also tried null
cookieAuthenticationOptions.AccessDeniedPath = ""; // also tried null
cookieAuthenticationOptions.LogoutPath = ""; // also tried null
});
Run Code Online (Sandbox Code Playgroud)
该代码似乎对默认重定向路径或行为没有影响.如何在Core 2.0中阻止这些重定向?
Kév*_*let 14
如https://github.com/aspnet/Announcements/issues/262中所述,您现在必须使用services.AddAuthentication()扩展名在全局级别配置默认方案处理程序.
为了防止Identity注册的cookie处理程序处理挑战,请使用DefaultChallengeScheme与不同处理程序相对应的方案(例如JWT承载处理程序)替换.
services.AddIdentity<User, IdentityRole>();
services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
});
Run Code Online (Sandbox Code Playgroud)
如果 - 无论出于何种原因 - 选择不同的处理程序不适合您,那么您将不得不使用services.ConfigureApplicationCookie()注册自定义CookieAuthenticationEvents.(On)RedirectToLogin事件来更改Identity返回"未经授权的响应"的方式.
这是返回401响应的示例:
services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToLogin = context =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4302 次 |
| 最近记录: |