Skip to content

Instantly share code, notes, and snippets.

@chadmichel
Created August 10, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadmichel/2ae28a30be803eb6944de90362f42eb5 to your computer and use it in GitHub Desktop.
Save chadmichel/2ae28a30be803eb6944de90362f42eb5 to your computer and use it in GitHub Desktop.
[ApiController]
[Route("[controller]")]
public class AuthController : Controller
{
// GET
[HttpGet]
public string Token()
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes("YOUR_KEY");
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Email, "bob@example.com")
}),
Expires = DateTime.UtcNow.AddMinutes(600),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment