public UsuariosController(SignInManager <IdentityUser> signInManager, UserManager <IdentityUser> userManager, IOptions <ConfiguracoesDeAutenticacao> appSettings) { this.userManager = userManager; configuracoesDeAutenticacao = appSettings.Value; }
public static async Task <String> GerarJwt(String email, UserManager <IdentityUser> userManager, ConfiguracoesDeAutenticacao configuracoesDeAutenticacao) { var user = await userManager.FindByEmailAsync(email); var identityClaims = new ClaimsIdentity(); identityClaims.AddClaims(await userManager.GetClaimsAsync(user)); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(configuracoesDeAutenticacao.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = identityClaims, Issuer = configuracoesDeAutenticacao.Emissor, Audience = configuracoesDeAutenticacao.ValidoEm, Expires = DateTime.UtcNow.AddHours(configuracoesDeAutenticacao.ExpiracaoHoras), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; return(tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor))); }