public override bool IsValid(object value) { if (value == null) { return(false); } var role = (string)value; if (string.IsNullOrEmpty(role)) { return(false); } var roles = IdentityRoles.GetRoles(); return(roles.Any(r => r.Equals(role, StringComparison.InvariantCultureIgnoreCase))); }
public static async Task CreateRoles(IServiceProvider serviceProvider, IConfiguration configuration, IOptions <IdentitySettings> identitySettings) { // adding custom roles var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var userManager = serviceProvider.GetRequiredService <UserManager <IdentityUser> >(); IdentityResult roleResult; foreach (var role in IdentityRoles.GetRoles()) { // creating the roles and seeding them to the database var roleExist = await roleManager.RoleExistsAsync(role); if (!roleExist) { roleResult = await roleManager.CreateAsync(new IdentityRole(role)); } } // creating a super user who could maintain the web app var powerUser = new IdentityUser { UserName = identitySettings.Value.SuperUser.Name, Email = identitySettings.Value.SuperUser.Email, EmailConfirmed = true }; string userPassword = identitySettings.Value.SuperUser.Password; var user = await userManager.FindByEmailAsync(identitySettings.Value.SuperUser.Email); if (user == null) { var createPowerUser = await userManager.CreateAsync(powerUser, userPassword); if (createPowerUser.Succeeded) { // here we tie new user to the "Admin" role await userManager.AddToRoleAsync(powerUser, IdentityRoles.Admin); } } }