-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtController.cs
More file actions
64 lines (58 loc) · 2.25 KB
/
JwtController.cs
File metadata and controls
64 lines (58 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using SimpleJwt4Core22.ViewModels;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace SimpleJwt4Core22.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class JwtController : ControllerBase
{
private readonly IConfiguration _configuration;
public JwtController(IConfiguration configuration)
{
_configuration = configuration;
}
[Route("maketoken")]
[HttpPost]
public ActionResult Login([FromBody] MakeTokenViewModel model)
{
// Simulate login or other failure:
if (model.Fail)
{
return BadRequest("JWT Creation Failure");
}
// Get configuration data
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
_configuration["Jwt:SigningKey"]));
int experyInMinutes = Convert.ToInt32(_configuration["Jwt:ExperyInMinutes"]);
string site = _configuration["Jwt:Site"];
JwtSecurityToken token = makeToken(model, signingKey, experyInMinutes, site);
// send it out as 200
return Ok(new JwtSecurityTokenHandler().WriteToken(token));
}
private static JwtSecurityToken makeToken(MakeTokenViewModel model, SymmetricSecurityKey signingKey, int experyInMinutes, string site)
{
// Create claims for any data I want to embed into the JWT
var claim = new[]
{
new Claim("name", model.UserName),
new Claim("id", model.Id.ToString()),
new Claim("role", model.Role)
};
// Create the sighed token
var token = new JwtSecurityToken(
issuer: site,
audience: site,
expires: DateTime.UtcNow.AddMinutes(experyInMinutes),
signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
claims: claim
);
return token;
}
}
}