使用 Razor 页面中的约定按区域按不同方案进行授权



我的 Razor Pages 应用程序的结构如下:

  • 页面
    • Index.cshtml
  • 地区
    • 管理
      • 页面
        • Index.cshtml
    • 应用程序接口
      • 页面
        • Index.cshtml

我想允许匿名访问任何非区域页面(/Pages/中的任何内容(。我想对管理区域中的所有页面使用 Windows 身份验证,并通过持有者令牌对 API 中的所有页面进行授权。

我可以直接在页面模型上使用授权属性并指定方案来执行此操作。

//Non-area example
[AllowAnonymous]
public class IndexModel : PageModel
//Admin example
[Authorize(AuthenticationSchemes = "Windows")]
public class IndexModel : PageModel
//API example
[Authorize(AuthenticationSchemes = "ApiKey")]
public class IndexModel : PageModel

然后,我可以为 3 个区域中的每一个创建一个基本页面模型,并从相应的基本页面模型继承每个区域的所有页面模型。

有没有办法使用约定来完成同样的事情?

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.???
    })

我已经解决了。诀窍是授权过滤器可以包含具有构造函数重载的方案。

var authorizeFilter = new AuthorizeFilter(new List<IAuthorizeData> {
    new AuthorizeAttribute()
    {
        AuthenticationSchemes = authenticationSchemes
    }
});

然后,我必须编写自己的IPageApplicationModelConvention,该公约将适用于区域级别。默认方法适用于文件夹和页面级别。我使用了Microsoft.AspNetCore.Mvc.RazorPages的源代码作为指导。

public class AreaModelConvention : IPageApplicationModelConvention
{
    private readonly string _areaName;
    private readonly Action<PageApplicationModel> _action;
    public AreaModelConvention(string areaName, Action<PageApplicationModel> action)
    {
        _areaName = areaName;
        _action = action;
    }
    public void Apply(PageApplicationModel model)
    {
        if(string.Equals(_areaName, model.AreaName, StringComparison.OrdinalIgnoreCase))
        {
            _action(model);
        }
    }
}

我写了一些PageConventionCollectionExtensions,这就是在Microsoft.AspNetCore.Mvc.RazorPages中完成这一切的方式。

public static class PageConventionCollectionExtensions
{
    public static PageConventionCollection RequireAuthenticationSchemesForArea(this PageConventionCollection conventions, string areaName, string authenticationSchemes)
    {
        if (conventions == null)
        {
            throw new ArgumentNullException(nameof(conventions));
        }
        if (string.IsNullOrEmpty(areaName))
        {
            throw new ArgumentException(nameof(areaName));
        }
        var authorizeFilter = new AuthorizeFilter(new List<IAuthorizeData> {
            new AuthorizeAttribute()
            {
                AuthenticationSchemes = authenticationSchemes
            }
        });
        conventions.AddAreaModelConvention(areaName, model => model.Filters.Add(authorizeFilter));
        return conventions;
    }
    public static IPageApplicationModelConvention AddAreaModelConvention(this ICollection<IPageConvention> pageConventions, string areaName, Action<PageApplicationModel> action)
    {
        if (action == null)
        {
            throw new ArgumentNullException(nameof(action));
        }
        var convention = new AreaModelConvention(areaName, action);
        pageConventions.Add(convention);
        return convention;
    }
}

最后我可以全部注册:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AllowAnonymousToNonareas();
        options.Conventions.RequireAuthenticationSchemesForArea("Admin", "Windows");
        options.Conventions.RequireAuthenticationSchemesForArea("Api", "ApiKey");
    })

注意:此处未定义AllowAnonymousToNonareas的代码,但它非常相似。我用这个应用方法创建了一个非区域模型约定:

public void Apply(PageApplicationModel model)
{
    if (model.AreaName == null)
    {
        _action(model);
    }
}

并编写了类似的扩展方法将其绑定在一起。

请记住为应用打开匿名身份验证和 Windows 身份验证。

最新更新