何时在ASP.NET Core中使用AuthorizeFilter

时间:2017-03-15 15:16:40

标签: asp.net-core asp.net-core-mvc asp.net-core-1.0

我开始研究ASP.NET核心,特别是授权。该模型发生了很大变化,我必须说,与上一次实施相比,我发现它有点令人困惑。

我看到各种帖子感到遗憾的是,现在无法将参数传递给Authorization属性构造函数,而现在我们已经有了Policy / Requirement / Handler。

那么AuthorizeFilter的用户案例究竟是什么?它与AuthorizeAttribute有何不同?我何时应该实现Requirement / Handler以及何时(或者我应该)实现AuthorizeFilter(看起来它可能更接近旧模型)?

我无法在网上找到任何详细说明此特定方面的资源,因此非常感谢任何启示。

1 个答案:

答案 0 :(得分:3)

AuthorizeFilter课程上方的文档说:

MVC recognizes the <see cref="T:Microsoft.AspNetCore.Authorization.AuthorizeAttribute" /> 
and adds an instance of this filter to the associated action or controller.

因此,当创建控制器时,AuthorizeFilter的实例会在AuthorizeAttribute出现时添加到控制器过滤器中。

基本上他们将装饰分开:AuthorizeAttribute来自实施:AuthorizeFilter

因此,如果您想要干净的代码,可以使用AuthorizeAttribute来装饰您的控制器类。

如果您想要更多可理解/逻辑代码,可以在控制器构造函数方法中将te AuthorizeFilter添加到过滤器。