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)
{
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));
}
}
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);
}