public async Task <AuthResponseDto> LoginAsync(LoginQuery userInput) { var foundUser = await _userManager.FindByNameAsync(userInput.UserName); if (foundUser == null) { throw new RestException(HttpStatusCode.Unauthorized); } var result = await _signInManager.CheckPasswordSignInAsync(foundUser, userInput.Password, false); if (result.Succeeded) { var rolesToUser = await _userManager.GetRolesAsync(foundUser); await _channelManager.PublishNextToChannel(ChannelEventEnum.Events, new LoginEvent(foundUser.UserName, result.Succeeded)); return(new AuthResponseDto(_tokenGenerator.CreateJwtToken(foundUser), foundUser.UserName, rolesToUser.ToList(), DateTime.Now.AddHours(JwtExpireTimeEnum.HoursToExpire.GetValue()) )); } throw new RestException(HttpStatusCode.Unauthorized); }
private async Task <AuthenticationResultModel> GetAuthenticationResultModel(ApplicationUser user, RefreshToken refreshToken) { //Generates new jwt var authenticationModel = new AuthenticationResultModel(); authenticationModel.IsAuthenticated = true; JwtSecurityToken jwtSecurityToken = await _tokenGenerator.CreateJwtToken(user); authenticationModel.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken); authenticationModel.Email = user.Email; authenticationModel.UserName = user.UserName; var rolesList = await _userManager .GetRolesAsync(user).ConfigureAwait(false); authenticationModel.Roles = rolesList.ToList(); authenticationModel.RefreshToken = refreshToken; return(authenticationModel); }
public async Task <AuthResponseDto> RegisterAsync(RegistrationCommand userInput) { var userFound = await _userManager.FindByNameAsync(userInput.Username); if (userFound != null) { throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" }); } var user = new User(userInput.Username, null, new PersonName("", "", ""), null); var created = await _userService.CreateUser(user, userInput.Password, userInput.Role); if (created) { return(new AuthResponseDto(_tokenGenerator.CreateJwtToken(user), user.UserName, new List <string> { userInput.Role }, DateTime.Now.AddHours(JwtExpireTimeEnum.HoursToExpire.GetValue()) )); } throw new SmartHubException("Problem Registering new User"); }