public string GenerateToken(ContextUserViewModel user) { try { JwtSecurityTokenHandler _tokenHandler = new JwtSecurityTokenHandler(); byte[] _key = Encoding.ASCII.GetBytes(Settings.Secret); SecurityTokenDescriptor _tokenDescriptor = new SecurityTokenDescriptor { Subject = _serviceAuth.GetClaimsIdentityByContextUser(user), Expires = DateTime.UtcNow.AddHours(3), NotBefore = DateTime.UtcNow, SigningCredentials = new SigningCredentials( new SymmetricSecurityKey(_key), SecurityAlgorithms.HmacSha256Signature) }; SecurityToken _generatedToken = _tokenHandler.CreateToken(_tokenDescriptor); return(_tokenHandler.WriteToken(_generatedToken)); } catch (Exception) { throw; } }
public IActionResult ActivateUser(int userId) { try { ContextUserViewModel _user = authService.GetLoggedUser(); if (_user == null || !UtilsService.IsAdmin(_user.Profile)) { return(Unauthorized()); } return(Ok(service.ActivateUser(userId))); } catch (Exception) { throw; } }
public ClaimsIdentity GetClaimsIdentityByContextUser(ContextUserViewModel user, string authenticationType = "Bearer") { try { return(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.PrimarySid, user.Id), new Claim(ClaimTypes.NameIdentifier, user.Email), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Role, user.Profile) }, authenticationType)); } catch (Exception) { throw; } }
public IActionResult GetUserLogged() { try { ContextUserViewModel _user = authService.GetLoggedUser(); if (_user == null) { return(Unauthorized()); } return(Ok(service.GetByProfile(int.Parse(_user.Profile)))); } catch (Exception) { throw; } }