如何授予“管理员”角色访问 .net core web 项目中所有页面的权限?

时间:2021-06-09 00:03:37

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

我有一个 .net core/5 razor pages 项目。

我正在使用授权标签和政策要求来限制对页面的访问。我想确保管理员默认可以访问所有页面,而无需使用 Authorize(Roles = "Admin") 将其添加到每个策略或每个页面上。

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.7.0/d3.min.js"></script>

1 个答案:

答案 0 :(得分:0)

<块引用>

我正在使用授权标签和政策要求来限制对 页。我想确保管理员可以通过以下方式访问所有页面 默认无需将其添加到每个策略或每个页面上 使用 Authorize(Roles = "Admin").

您可以设置 Razor Pages 授权约定,将 admin 相关页面放在 admin 文件夹中,然后使用以下 AuthorizePage 约定在指定路径的页面中添加 AuthorizeFilter:

services.AddRazorPages(options =>
{
    // specify an authorization policy for the admin folder.
    options.Conventions.AuthorizeFolder("/admin", "AtLeast21");

    //Besides, you can also use the AuthorizePage method to specify an authorization policy for the admin related pages.
    //specify an authorization policy for the page.
    //options.Conventions.AuthorizePage("/Contact", "AtLeast21");
});

更多详细信息,请参阅 AuthorizePageAuthorizeFolderRazor Pages authorization conventions in ASP.NET Core