public JsonWebToken CreateAccessToken(string userName, Guid userId) { var claims = new[] { new Claim(ClaimTypes.Name, userName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(ClaimTypes.NameIdentifier, userId.ToString()) }; return(new JsonWebToken { AccessToken = _jwtHandler.CreateAccessToken(claims), RefreshToken = _refreshHandler.CreateRefreshToken(userName, userId), }); }
public AuthenticationResponse LoginAsync(UserLoginModel model) { ArgumentGuard.NotNullOrEmpty(model.EmailAddress, nameof(model.EmailAddress)); // ArgumentGuard.NotNullOrEmpty(model.ReturnUrl, nameof(model.ReturnUrl)); var email = model.EmailAddress.Trim().ToLower(); var userEntities = _userRepository.GetMany(u => u.EmailAddress == email); var userEntity = userEntities.SingleOrDefault(); if (userEntity.IsNull()) { throw new Exception($"No user with email {email} exists"); } var hashedPassword = _cryptoService.Hash(model.Password, userEntity.LoginProfile.Salt, 5323); if (userEntity.LoginProfile.Password != hashedPassword) { throw new Exception("Incorrect Password"); } var user = Mapper.Map <User>(userEntity); var token = _jwtHandler.CreateAccessToken(user); return(new AuthenticationResponse { Token = token, User = user }); }
public async Task <AuthenticationResponse> LoginWithPasswordAsync(string email, string password) { ArgumentGuard.NotNullOrWhiteSpace(email, nameof(email)); ArgumentGuard.NotNullOrWhiteSpace(password, nameof(password)); var user = await LoginAsync(email, password); var token = _jwtHandler.CreateAccessToken(user); return(Response(user, token)); }