diff --git a/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml b/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml index 6fbfc8f..779b1b4 100644 --- a/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml +++ b/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml @@ -27,6 +27,11 @@ +
+ +
diff --git a/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml.cs b/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml.cs index ae96eed..cdb823c 100644 --- a/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -17,7 +17,9 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; @@ -102,6 +104,10 @@ public class InputModel [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } + + public string? Role { get; set; } + [ValidateNever] + public IEnumerable RoleList { get; set; } } @@ -115,6 +121,16 @@ public async Task OnGetAsync(string returnUrl = null) _roleManager.CreateAsync(new IdentityRole(SD.Role_Employee)).GetAwaiter().GetResult(); } + Input = new() + { + RoleList = _roleManager.Roles.Select(x => x.Name).Select(i => new SelectListItem + { + Text = i, + Value = i + }) + }; + + ReturnUrl = returnUrl; ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); } @@ -135,6 +151,15 @@ public async Task OnPostAsync(string returnUrl = null) { _logger.LogInformation("User created a new account with password."); + if (!String.IsNullOrEmpty(Input.Role)) + { + await _userManager.AddToRoleAsync(user, Input.Role); + } + else + { + await _userManager.AddToRoleAsync(user, SD.Role_Customer); + } + var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); diff --git a/BulkyWeb/Program.cs b/BulkyWeb/Program.cs index 70d7f6e..eebcaa7 100644 --- a/BulkyWeb/Program.cs +++ b/BulkyWeb/Program.cs @@ -13,7 +13,7 @@ builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); -builder.Services.AddIdentity().AddEntityFrameworkStores(); +builder.Services.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders(); builder.Services.AddRazorPages(); builder.Services.AddScoped(); builder.Services.AddScoped();