Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Razor页面中添加对角色的支持和填充角色?_C#_Asp.net Core_Razor Pages_Asp.net Core Identity - Fatal编程技术网

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

C# 如何在Razor页面中添加对角色的支持和填充角色?,c#,asp.net-core,razor-pages,asp.net-core-identity,C#,Asp.net Core,Razor Pages,Asp.net Core Identity,我试图弄清楚如何在Razor Pages应用程序中启用和填充角色 通过遵循各种教程,Startup.cs中的ConfigureServices如下所示: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Co

我试图弄清楚如何在Razor Pages应用程序中启用和填充角色

通过遵循各种教程,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));
    });
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity(选项=>
{
options.SignIn.RequireConfirmedAccount=false;
})
.AddRoles()
.AddEntityFrameworkStores();
services.AddRazorPages();
//将默认身份验证策略设置为要求对用户进行身份验证
services.AddControllers(配置=>
{
var policy=new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()文件
.Build();
config.Filters.Add(新的授权过滤器(策略));
});
}
但是对
AddDefaultIdentity
的调用引发了一个异常

System.InvalidOperationException:“只能使用从IdentityRole派生的角色调用AddEntityFrameworkStores。”


有人能看到我遗漏了什么吗?此外,我还想知道如何填充角色。

您可能会将应用程序中的自定义角色(管理员、经理等)与IdentityUser混淆

IdentityUser
是应用程序中用户记录的基本模型

如果要向用户模型添加自定义属性,请创建一个从
IdentityUser
继承的新类,并添加新属性。这个新类通常被称为
ApplicationUser
,但可以被任意调用

我建议遵循一个教程。下面是Microsoft文档中的一个,它经历了许多场景,并且有源代码


将您的
IdentityUser
更改为
IdentityRole
,如下所示:

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

services.AddDefaultIdentity

在你的问题上非常相似:@DenisStukalov:我确实看到了。但他使用的方法与VisualStudio创建的不同,我不知道为什么。我找不到关于这方面的好教程。它应该是
.AddRoles()
而不是IdentityUser。我认为
IdentityUser
是应用程序中用户记录的基本模型?是的,你是对的。我已经更正了答案,谢谢@Jonathanwood这很有效,谢谢。有填充我的应用程序角色的好例子吗?嗨@JonathanWood,填充你的角色是什么意思?将它们填充到数据库中?请参阅。或填充selectlist中的角色?