简体   繁体   English

如何在 Razor 页面中添加对角色的支持和填充角色?

[英]How do I add support for and populate roles in Razor Pages?

I'm trying to figure out how to enable and populate roles in my Razor Pages application.我试图弄清楚如何在我的 Razor Pages 应用程序中启用和填充角色。

By following various tutorials, ConfigureServices in Startup.cs looks like this:通过遵循各种教程, Startup.cs中的ConfigureServices如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<ApplicationUser>(options =>
        {
            options.SignIn.RequireConfirmedAccount = false;
        })
        .AddRoles<ApplicationUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddRazorPages();

    // Set the default authentication policy to require users to be authenticated
    services.AddControllers(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    });
}

But the call to AddDefaultIdentity raises an exception.但是对AddDefaultIdentity的调用会引发异常。

System.InvalidOperationException: 'AddEntityFrameworkStores can only be called with a role that derives from IdentityRole.' System.InvalidOperationException:“只能使用派生自 IdentityRole 的角色调用 AddEntityFrameworkStores。”

Can anyone see what I'm missing here?谁能看到我在这里缺少的东西? Also, I really want to know how to populate the roles.另外,我真的很想知道如何填充角色。

Change your IdentityUser to IdentityRole like below:将您的IdentityUser更改为IdentityRole ,如下所示:

services.AddDefaultIdentity<ApplicationUser>(options =>
        {
            options.SignIn.RequireConfirmedAccount = false;
        })
        .AddRoles<IdentityRole>()  //change this
        .AddEntityFrameworkStores<ApplicationDbContext>();

Reference:参考:

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-3.1#add-role-services-to-identity https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-3.1#add-role-services-to-identity

You might be confusing your custom roles in the application (Admin, Manager, etc) with IdentityUser.您可能会将应用程序中的自定义角色(管理员、经理等)与 IdentityUser 混淆。

IdentityUser is the basic model for the User records in the application. IdentityUser是应用程序中用户记录的基本 model。

If you want to add custom properties to the User model then create a new class that inherits from IdentityUser and add new properties.如果要向用户 model 添加自定义属性,则创建一个继承自IdentityUser的新 class 并添加新属性。 This new class is often called ApplicationUser but can be called anything you want.这个新的 class 通常被称为ApplicationUser但可以任意命名。

Docs on customizing user model 自定义用户 model 文档

I recommend following one tutorial.我建议遵循一个教程。 Here is one from the Microsoft documentation that goes through a number of scenarios and has source code as well.这是 Microsoft 文档中的一个,它经历了许多场景并且也有源代码。

Official tutorial on authorization and roles 授权和角色官方教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM