public void Register(string email, string password, RoleDto role) { var user = _userRepository.Get(email); if (user != null) { throw new Exception($"User '{email}' already exists."); } var userRole = (Role)Enum.Parse(typeof(Role), role.ToString(), true); user = new User(email, password, userRole); _userRepository.Add(user); }
public JwtDto CreateToken(Guid userId, RoleDto role) { var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Value.Secret)), SecurityAlgorithms.HmacSha256); var claims = new Claim[] { new Claim(JwtRegisteredClaimNames.UniqueName, userId.ToString()), new Claim(ClaimTypes.Role, role.ToString()), }; var token = new JwtSecurityToken( claims: claims, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddDays(30), signingCredentials: signingCredentials); var tokenString = new JwtSecurityTokenHandler().WriteToken(token); return(new JwtDto { Token = tokenString }); }