6
6
using System . Security . Claims ;
7
7
using System . Text ;
8
8
using System . Threading . Tasks ;
9
+ using Microsoft . AspNetCore . Authorization ;
9
10
using Microsoft . AspNetCore . Http ;
10
11
using Microsoft . AspNetCore . Mvc ;
11
12
using Microsoft . Extensions . Options ;
12
13
using Newtonsoft . Json . Linq ;
13
14
14
15
namespace APIJSON . NET . Controllers
15
16
{
16
- [ Route ( "api/[controller]" ) ]
17
+ [ Route ( "api/[controller]/[action] " ) ]
17
18
[ ApiController ]
19
+ [ Authorize ]
18
20
public class TokenController : ControllerBase
19
21
{
20
22
private DbContext db ;
@@ -24,8 +26,9 @@ public TokenController(DbContext _db, IOptions<TokenAuthConfiguration> configura
24
26
_configuration = configuration ;
25
27
db = _db ;
26
28
}
27
- [ HttpGet ( "/token" ) ]
28
- public IActionResult Create ( TokenInput input )
29
+ [ HttpPost ( "/token" ) ]
30
+ [ AllowAnonymous ]
31
+ public IActionResult Create ( [ FromBody ] TokenInput input )
29
32
{
30
33
JObject ht = new JObject ( ) ;
31
34
ht . Add ( "code" , "200" ) ;
@@ -45,13 +48,19 @@ public IActionResult Create(TokenInput input)
45
48
return Ok ( ht ) ;
46
49
}
47
50
var identity = new ClaimsIdentity ( ) ;
48
- identity . AddClaim ( new Claim ( ClaimTypes . NameIdentifier , us . userId . ToString ( ) ) ) ;
49
- identity . AddClaim ( new Claim ( ClaimTypes . Role , us . roleCode ) ) ;
50
- identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Sub , input . username ) ) ;
51
- identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Jti , Guid . NewGuid ( ) . ToString ( ) ) ) ;
52
- identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Iat , DateTimeOffset . Now . ToUnixTimeSeconds ( ) . ToString ( ) , ClaimValueTypes . Integer64 ) ) ;
53
-
54
- var accessToken = CreateAccessToken ( identity . Claims . ToList ( ) ) ;
51
+ identity . AddClaim ( new Claim ( ClaimTypes . NameIdentifier , us . userId . ToString ( CultureInfo . InvariantCulture ) ) ) ;
52
+ identity . AddClaim ( new Claim ( ClaimTypes . Name , us . userId . ToString ( CultureInfo . InvariantCulture ) ) ) ;
53
+ identity . AddClaim ( new Claim ( ClaimTypes . Role , us . roleCode . ToString ( CultureInfo . InvariantCulture ) ) ) ;
54
+ var claims = identity . Claims . ToList ( ) ;
55
+
56
+ claims . AddRange ( new [ ]
57
+ {
58
+ new Claim ( JwtRegisteredClaimNames . Sub , us . userId . ToString ( CultureInfo . InvariantCulture ) ) ,
59
+ new Claim ( JwtRegisteredClaimNames . Jti , Guid . NewGuid ( ) . ToString ( ) ) ,
60
+ new Claim ( JwtRegisteredClaimNames . Iat , DateTimeOffset . Now . ToUnixTimeSeconds ( ) . ToString ( ) , ClaimValueTypes . Integer64 )
61
+ } ) ;
62
+
63
+ var accessToken = CreateAccessToken ( claims ) ;
55
64
56
65
var data = new AuthenticateResultModel ( )
57
66
{
@@ -62,6 +71,11 @@ public IActionResult Create(TokenInput input)
62
71
ht . Add ( "data" , JToken . FromObject ( data ) ) ;
63
72
return Ok ( ht ) ;
64
73
}
74
+ [ HttpGet ]
75
+ public IActionResult GetRole ( )
76
+ {
77
+ return Ok ( User . Identity . Name ) ;
78
+ }
65
79
private string CreateAccessToken ( IEnumerable < Claim > claims , TimeSpan ? expiration = null )
66
80
{
67
81
var now = DateTime . UtcNow ;
0 commit comments