Skip to content

Commit

Permalink
Added option to assign role to user on registration page.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliarmaganuygun committed Oct 14, 2024
1 parent 4956ea9 commit 5bafd5e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<label asp-for="Input.ConfirmPassword">Confirm Password</label>
<span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
</div>
<div class="form-floating mb-3">
<select asp-for="Input.Role" asp-items="@Model.Input.RoleList" class="form-select">
<option disabled selected>Select Role</option>
</select>
</div>
<button id="registerSubmit" type="submit" class="w-100 btn btn-lg btn-primary">Register</button>
</form>
</div>
Expand Down
25 changes: 25 additions & 0 deletions BulkyWeb/Areas/Identity/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<SelectListItem> RoleList { get; set; }
}


Expand All @@ -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();
}
Expand All @@ -135,6 +151,15 @@ public async Task<IActionResult> 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));
Expand Down
2 changes: 1 addition & 1 deletion BulkyWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
builder.Services.AddRazorPages();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IEmailSender, EmailSender>();
Expand Down

0 comments on commit 5bafd5e

Please sign in to comment.