private void AddLawyerUser() { Task.Run(async() => { //Check if Role Exists var basicRole = new IdentityRole(RoleConstants.BasicRole); var basicRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.BasicRole); if (basicRoleInDb == null) { await _roleManager.CreateAsync(basicRole); _logger.LogInformation("Seeded Basic Role."); } //Check if User Exists var basicUser = new LLCUser { FirstName = "Joe", LastName = "Lawyer", Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, PhoneNumberConfirmed = true, CreatedOn = DateTime.Now, IsActive = true }; var basicUserInDb = await _userManager.FindByEmailAsync(basicUser.Email); if (basicUserInDb == null) { await _userManager.CreateAsync(basicUser, UserConstants.DefaultPassword); await _userManager.AddToRoleAsync(basicUser, RoleConstants.BasicRole); _logger.LogInformation("Seeded User with Basic Role."); } }).GetAwaiter().GetResult(); }
private async Task <IEnumerable <Claim> > GetClaimsAsync(LLCUser user) { var userClaims = await _userManager.GetClaimsAsync(user); var roles = await _userManager.GetRolesAsync(user); var roleClaims = new List <Claim>(); var permissionClaims = new List <Claim>(); foreach (var role in roles) { roleClaims.Add(new Claim(ClaimTypes.Role, role)); var thisRole = await _roleManager.FindByNameAsync(role); var allPermissionsForThisRoles = await _roleManager.GetClaimsAsync(thisRole); permissionClaims.AddRange(allPermissionsForThisRoles); } var claims = new List <Claim> { new(ClaimTypes.NameIdentifier, user.Id), new(ClaimTypes.Email, user.Email), new(ClaimTypes.Name, user.FirstName), new(ClaimTypes.Surname, user.LastName), new(ClaimTypes.MobilePhone, user.PhoneNumber ?? string.Empty) }
public async Task <IResult> RegisterAsync(RegisterRequest request, string origin) { var userWithSameUserName = await _userManager.FindByNameAsync(request.UserName); if (userWithSameUserName != null) { return(await Result.FailAsync($"{_localizer["Username"]} '{request.UserName}' {_localizer["is already taken."]}")); } var user = new LLCUser { Email = request.Email, FirstName = request.FirstName, LastName = request.LastName, UserName = request.UserName, PhoneNumber = request.PhoneNumber, IsActive = request.ActivateUser, EmailConfirmed = request.AutoConfirmEmail }; var userWithSameEmail = await _userManager.FindByEmailAsync(request.Email); if (userWithSameEmail == null) { var result = await _userManager.CreateAsync(user, request.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, RoleConstants.BasicRole); if (!request.AutoConfirmEmail) { var verificationUri = await SendVerificationEmail(user, origin); var mailRequest = new MailRequest { From = "*****@*****.**", To = user.Email, Body = $"{_localizer["Please confirm your account by"]} <a href='{verificationUri}'>{_localizer["clicking here"]}</a>.", Subject = _localizer["Confirm Registration"] }; BackgroundJob.Enqueue(() => _mailService.SendAsync(mailRequest)); return(await Result <string> .SuccessAsync(user.Id, message : _localizer[$"User Registered. Please check your Mailbox to verify!"])); } return(await Result <string> .SuccessAsync(user.Id, message : _localizer[$"User Registered"])); } else { return(await Result.FailAsync(result.Errors.Select(a => a.Description).ToList())); } } else { return(await Result.FailAsync($"{_localizer["Email"]} {request.Email } {_localizer["is already registered."]}")); } }
private async Task <string> SendVerificationEmail(LLCUser user, string origin) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var route = "api/identity/user/confirm-email/"; var endpointUri = new Uri(string.Concat($"{origin}/", route)); var verificationUri = QueryHelpers.AddQueryString(endpointUri.ToString(), "userId", user.Id); verificationUri = QueryHelpers.AddQueryString(verificationUri, "code", code); return(verificationUri); }
private void AddAdministrator() { Task.Run(async() => { //Check if Role Exists var adminRole = new IdentityRole(RoleConstants.AdministratorRole); var adminRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.AdministratorRole); if (adminRoleInDb == null) { await _roleManager.CreateAsync(adminRole); _logger.LogInformation("Seeded Administrator Role."); } //Check if User Exists var superUser = new LLCUser { FirstName = "James", LastName = "Admin", Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, PhoneNumberConfirmed = true, CreatedOn = DateTime.Now, IsActive = true }; var superUserInDb = await _userManager.FindByEmailAsync(superUser.Email); if (superUserInDb == null) { await _userManager.CreateAsync(superUser, UserConstants.DefaultPassword); var result = await _userManager.AddToRoleAsync(superUser, RoleConstants.AdministratorRole); if (result.Succeeded) { await _roleManager.GeneratePermissionClaimByModule(adminRole, PermissionModules.Users); await _roleManager.GeneratePermissionClaimByModule(adminRole, PermissionModules.Roles); await _roleManager.GeneratePermissionClaimByModule(adminRole, PermissionModules.Products); await _roleManager.GeneratePermissionClaimByModule(adminRole, PermissionModules.Brands); } _logger.LogInformation("Seeded User with Administrator Role."); } }).GetAwaiter().GetResult(); }
private async Task <string> GenerateJwtAsync(LLCUser user) { var token = GenerateEncryptedToken(GetSigningCredentials(), await GetClaimsAsync(user)); return(token); }