public IActionResult Login([FromBody] InternalUserLoginRequest internalUserLoginRequest) { try { InternalUserResponse internalUser = _internalUserService.GetInternalUserInformation(internalUserLoginRequest); if (internalUser == null) { return(Unauthorized()); } var tokenString = _tokenHelper.GenerateToken(internalUser); var organization = _organizationService.GetOrganizationByEmail(internalUser.Email); return(Ok(new { Token = tokenString, IsOrganization = internalUser.IsOrganization, Organization = organization })); } catch (Exception ex) { return(Unauthorized(ex.Message)); } }
public string GenerateToken(InternalUserResponse administrator) { var issuerSigningKey = tokenConfiguration.IssuerSigningKey; var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(issuerSigningKey)); var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, administrator.Key), new Claim(ClaimTypes.Name, $"{administrator.Email} {administrator.Key}"), }; var tokenOptions = new JwtSecurityToken( issuer: tokenConfiguration.ApiURL, // localhost de este audience: tokenConfiguration.WebAppRootURL, // localhost 4200 claims: claims, expires: DateTime.Now.AddMinutes(tokenConfiguration.TokenExpirationMinutes), signingCredentials: signinCredentials ); return(new JwtSecurityTokenHandler().WriteToken(tokenOptions)); }