有关ASP.NET核心用户角色的问题 [英] Problem regarding ASP.NET core user roles

查看:119
本文介绍了有关ASP.NET核心用户角色的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我正在尝试将单个主持人添加到我的ASP.NET Core 2.0网站。除此之外,所有其他用户都将是简单的用户。基本上我需要的是两个角色,一个叫User,一个叫Moderator。到目前为止,我提出了一个解决方案,它提出了一个奇怪的错误。问题是,在第一次await调用(我的方法是异步),该方法没有做任何事情它似乎被困在那里,即使我的网站成功启动。如果有人可以帮助我,我会很高兴。



我尝试过:



到目前为止,我所做的是在项目的Startup类中创建一个方法来创建我的角色。然后我从Startup类的Configure方法中调用此方法。这是方法:



私有异步任务CreateRoles(IServiceProvider serviceProvider)
{
//初始化角色管理器
var roleManager = serviceProvider.GetRequiredService< RoleManager< IdentityRole>>();

//如果主持人角色已经存在,我们得到(这正是方法卡住的地方)
var moderatorRoleExists = await roleManager.RoleExistsAsync(Roles.Moderator);
//如果用户角色已经存在,我们得到
var userRoleExists = await roleManager.RoleExistsAsync(Roles.User);

//如果主持人角色不存在
if(!moderatorRoleExists)
{
//创建角色并将它们播种到数据库:
await roleManager.CreateAsync(new IdentityRole(Roles.Moderator));
}

//如果主持人用户不存在
if(!userRoleExists)
{
//创建角色并将其播种到数据库:
await roleManager.CreateAsync(new IdentityRole(Roles.User));
}
}





这就是我从配置方法中调用它的方式:



 public void配置(IApplicationBuilder app,IHostingEnvironment env,IServiceProvider serviceProvider)
{
if(env.IsDevelopment() )
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler(/ Home / Error);
}

app.UseStaticFiles();

app.UseAuthentication();

app.UseMvc(routes =>
{
routes.MapRoute(
name:default,
template:{controller = Home } / {action = Index} / {id?});
});

CreateRoles(serviceProvider);
}

解决方案

我实际上设法找到了解决方案。解决方案是在调用CreateRoles方法时使用Wait方法。所以电话应该是这样的:



 CreateRoles(serviceProvider).Wait(); 


Hello! I am trying to add a single moderator to my ASP.NET Core 2.0 website. All other users are going to be simple users except this one. So basically what I need is two roles, one called User, and one called Moderator. So far I came up with a solution, that comes up with a weird bug. The problem is that in the first await call (my method is asynchronous), the method doesn't do anything It seems to be stuck there, even though my website is getting launched successfully. If someone could help me with that, I would be glad.

What I have tried:

What I have done so far, is creating a method on the Startup class of my project, to create my roles. Then I call this method from the Configure method of the Startup class. This is the method:

private async Task CreateRoles(IServiceProvider serviceProvider)
{
    //Initialize role manager
    var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

    //We get if the moderator role already exists (This is exactly where the method is getting stuck)
    var moderatorRoleExists = await roleManager.RoleExistsAsync(Roles.Moderator);
    //We get if the user role already exists
    var userRoleExists = await roleManager.RoleExistsAsync(Roles.User);

    // if the moderator role doesn't exist
    if (!moderatorRoleExists)
    {
        //create the role and seed them to the database:
        await roleManager.CreateAsync(new IdentityRole(Roles.Moderator));
    }

    // if the moderator user doesn't exist
    if (!userRoleExists)
    {
        //create the role and seed them to the database:
        await roleManager.CreateAsync(new IdentityRole(Roles.User));
    }
}



And this is how I call it from the Configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
    if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    CreateRoles(serviceProvider);
}

解决方案

I actually managed to find a solution. The solution is to use the Wait method when calling the CreateRoles method. So the call, should be like that:

CreateRoles(serviceProvider).Wait();


这篇关于有关ASP.NET核心用户角色的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆