public async Task <User> Handle(Command request, CancellationToken cancellationToken) { if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync()) { throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email is already in use!" }); } if (await _context.Users.Where(x => x.UserName == request.Username).AnyAsync()) { throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" }); } var user = new WoWUser { DisplayName = request.DisplayName, Email = request.Email, UserName = request.Username }; var result = await _userManager.CreateAsync(user, request.Password); if (result.Succeeded) { return(new User { DisplayName = user.DisplayName, Token = _jwtGenerator.CreateToken(user), Username = user.UserName, Image = null }); } throw new Exception("Problem creating new user!"); }
public string CreateToken(WoWUser user) { var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.NameId, user.UserName), }; // generate signing credentials var credentials = new SigningCredentials(_key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(7), SigningCredentials = credentials }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new WoWUser { UserName = Input.Email, Email = Input.Email }; bool adminRoleExists = await _roleManager.RoleExistsAsync("Admin"); bool userRoleExists = await _roleManager.RoleExistsAsync("User"); if (!adminRoleExists || !userRoleExists) { var roleAdmin = new IdentityRole { Name = "Admin" }; await _roleManager.CreateAsync(roleAdmin); var roleUser = new IdentityRole { Name = "User" }; await _roleManager.CreateAsync(roleUser); } var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { var adminExists = await _userManager.GetUsersInRoleAsync("Admin"); if (adminExists.Count == 0) { await _userManager.AddToRoleAsync(user, "Admin"); } else { await _userManager.AddToRoleAsync(user, "User"); } _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); //await _signInManager.SignInAsync(user, isPersistent: false); return(RedirectToAction("VerifyEmail", "Home")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }