66using System . Security . Claims ;
77using System . Text ;
88using System . Threading . Tasks ;
9+ using Microsoft . AspNetCore . Authorization ;
910using Microsoft . AspNetCore . Http ;
1011using Microsoft . AspNetCore . Mvc ;
1112using Microsoft . Extensions . Options ;
1213using Newtonsoft . Json . Linq ;
1314
1415namespace APIJSON . NET . Controllers
1516{
16- [ Route ( "api/[controller]" ) ]
17+ [ Route ( "api/[controller]/[action] " ) ]
1718[ ApiController ]
19+ [ Authorize ]
1820public class TokenController : ControllerBase
1921{
2022private DbContext db ;
@@ -24,8 +26,9 @@ public TokenController(DbContext _db, IOptions<TokenAuthConfiguration> configura
2426_configuration = configuration ;
2527db = _db ;
2628}
27- [ HttpGet ( "/token" ) ]
28- public IActionResult Create ( TokenInput input )
29+ [ HttpPost ( "/token" ) ]
30+ [ AllowAnonymous ]
31+ public IActionResult Create ( [ FromBody ] TokenInput input )
2932{
3033JObject ht = new JObject ( ) ;
3134ht . Add ( "code" , "200" ) ;
@@ -45,13 +48,19 @@ public IActionResult Create(TokenInput input)
4548return Ok ( ht ) ;
4649}
4750var 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 ) ;
5564
5665var data = new AuthenticateResultModel ( )
5766{
@@ -62,6 +71,11 @@ public IActionResult Create(TokenInput input)
6271ht . Add ( "data" , JToken . FromObject ( data ) ) ;
6372return Ok ( ht ) ;
6473}
74+ [ HttpGet ]
75+ public IActionResult GetRole ( )
76+ {
77+ return Ok ( User . Identity . Name ) ;
78+ }
6579private string CreateAccessToken ( IEnumerable < Claim > claims , TimeSpan ? expiration = null )
6680{
6781var now = DateTime . UtcNow ;
0 commit comments