public async Task <ServiceResponse <int> > Register(CharUser user, string password) { ServiceResponse <int> response = new ServiceResponse <int>(); if (await UserExists(user.UserName)) { response.success = false; response.message = "User already exists."; return(response); } CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _context.CharUsers.Add(user); await _context.SaveChangesAsync(); response.data = user.Id; return(response); }
private string CreateToken(CharUser user) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.UserName) }; var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_configuration.GetSection("AppSettings:Token").Value)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokendDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = System.DateTime.Now.AddDays(1), SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokendDescriptor); return(tokenHandler.WriteToken(token)); }