在操作和控制器中具有不同身份验证方案的授权属性的行为是什么
What is the behavior of Authorize attribute with different authentication scheme in action and controller
[Authorize(AuthenticationSchemes = AuthenticationSchemes.CookieAuthenticationScheme)]
public class MyController : ControllerBase
{
[HttpPost("ui")]
[ProducesResponseType(
(int) HttpStatusCode.Created)]
public async Task Action1()
{
}
[HttpPost]
[ProducesResponseType(
(int)HttpStatusCode.Created)]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.JwtAuthenticationScheme)]
public async Task Action2()
{
}
}
我有这个控制器,其中 Action2 具有 Authorize 属性,其身份验证方案与 Controller 中的不同。但是当我使用有效的 cookie 身份验证但无效的身份验证令牌调用 Action2 时,Action2 也被授权 - 我期待得到 401/Unauthorized 响应。
这是有意为之的行为吗?
使用 Asp.net 核心 2.2
在 ASP.NET Core 2.1 之前,所有策略都将单独评估,并且都需要得到满足。
这在 ASP.NET Core 2.1 中发生了变化,指出此行为是无意的。在那个版本中,策略将被组合,这样如果至少满足一个,请求的授权要求也被满足。
该团队在 MvcOptions
上公开了一个名为 AllowCombiningAuthorizeFilters
的新 属性,以防人们依赖该行为。
参见:
- 与此相关的官方文档属性:https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.allowcombiningauthorizefilters?view=aspnetcore-2.2
- 来自 Brock Allen 的博客 post 揭露了一个场景:https://brockallen.com/2018/07/15/beware-the-combined-authorize-filter-mechanics-in-asp-net-core-2-1/
如果您想恢复到以前的行为,可以在 Startup
class 中使用以下内容:
app.AddMvc(options =>
{
options.AllowCombiningAuthorizeFilters = false;
});
[Authorize(AuthenticationSchemes = AuthenticationSchemes.CookieAuthenticationScheme)]
public class MyController : ControllerBase
{
[HttpPost("ui")]
[ProducesResponseType(
(int) HttpStatusCode.Created)]
public async Task Action1()
{
}
[HttpPost]
[ProducesResponseType(
(int)HttpStatusCode.Created)]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.JwtAuthenticationScheme)]
public async Task Action2()
{
}
}
我有这个控制器,其中 Action2 具有 Authorize 属性,其身份验证方案与 Controller 中的不同。但是当我使用有效的 cookie 身份验证但无效的身份验证令牌调用 Action2 时,Action2 也被授权 - 我期待得到 401/Unauthorized 响应。
这是有意为之的行为吗?
使用 Asp.net 核心 2.2
在 ASP.NET Core 2.1 之前,所有策略都将单独评估,并且都需要得到满足。
这在 ASP.NET Core 2.1 中发生了变化,指出此行为是无意的。在那个版本中,策略将被组合,这样如果至少满足一个,请求的授权要求也被满足。
该团队在 MvcOptions
上公开了一个名为 AllowCombiningAuthorizeFilters
的新 属性,以防人们依赖该行为。
参见:
- 与此相关的官方文档属性:https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.allowcombiningauthorizefilters?view=aspnetcore-2.2
- 来自 Brock Allen 的博客 post 揭露了一个场景:https://brockallen.com/2018/07/15/beware-the-combined-authorize-filter-mechanics-in-asp-net-core-2-1/
如果您想恢复到以前的行为,可以在 Startup
class 中使用以下内容:
app.AddMvc(options =>
{
options.AllowCombiningAuthorizeFilters = false;
});