//TODO resolve config service to get token value from appsettings.json directly public static object GetSecurityToken(DataProviders.Entities.User userFromDb, string tokenValue) { var claims = new [] { new Claim(ClaimTypes.NameIdentifier, userFromDb.Id.ToString()), new Claim(ClaimTypes.Name, userFromDb.Username), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenValue)); //key hashing (encryption) var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor() { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = credentials, }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(new { token = tokenHandler.WriteToken(token) }); }
public async Task <IActionResult> Register(UserToRegisterModel usrModel) { //TODO Validate request if (await _userRepository.UserExists(usrModel.UserName.ToLower())) { return(BadRequest("User name already exists")); } var userToCreate = new DataProviders.Entities.User { Username = usrModel.UserName.ToLower() }; var createdUser = await _userRepository.Register(userToCreate, usrModel.Password); // return CreatedAtRoute() return(StatusCode(201)); }