Hi @Ronald Rex
I was wondering when a user Registers to my website can I assign them to a default role with the minimal privileges until I choose to add them to another role? Just wondering does the Startup.cs have some sort of Configuration that handles this.
As far as I know, there doesn't have this method to set the default role in the Startup.cs configuration.
To set the default role when register the user (using the Asp.net core Identity), I suggest you could refer to the following steps:
- Add Role services to Identity Append
AddRoles
to add Role services:public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<IdentityUser>() .AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddControllersWithViews(); services.AddRazorPages(); }
- Custom the Identity Register page: From Solution Explorer, right-click on the project > Add > New Scaffolded Item.
From the left pane of the Add New Scaffolded Item dialog, select Identity > Add.
In the Add Identity dialog, select the options you want, then click the Add button. Here we need to use the Register page: Then, we can find the Register.cshtml page from the Areas/Identity/Pages/Account folder.
Open the Register.cshtml.cs page, and inject the RoleManager, it could use to create/find/delete roles.
Then, in the OnPostAsync method, after user created success, we could use RoleManager to find the default role, and then, use UserManager to add user to Role. Code like this:[AllowAnonymous] public class RegisterModel : PageModel { private readonly SignInManager<IdentityUser> _signInManager; private readonly UserManager<IdentityUser> _userManager; private readonly ILogger<RegisterModel> _logger; private readonly IEmailSender _emailSender; private readonly RoleManager<IdentityRole> _roleManager; public RegisterModel( UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, ILogger<RegisterModel> logger, RoleManager<IdentityRole> roleManager, IEmailSender emailSender) { _userManager = userManager; _signInManager = signInManager; _logger = logger; _roleManager = roleManager; _emailSender = emailSender; }
Finally, if we register a new user, the result as below:public async Task<IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { var defaultrole = _roleManager.FindByNameAsync("Default").Result; if (defaultrole != null) { IdentityResult roleresult = await _userManager.AddToRoleAsync(user, defaultrole.Name); }
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion