4
4
using System . IdentityModel . Tokens . Jwt ;
5
5
using System . Linq ;
6
6
using System . Security . Claims ;
7
+ using System . Text ;
7
8
using System . Threading . Tasks ;
8
9
using Microsoft . AspNetCore . Http ;
9
10
using Microsoft . AspNetCore . Mvc ;
@@ -16,29 +17,37 @@ namespace APIJSON.NET.Controllers
16
17
[ ApiController ]
17
18
public class TokenController : ControllerBase
18
19
{
19
- private DbOptions _options ;
20
+ private DbContext db ;
20
21
private readonly IOptions < TokenAuthConfiguration > _configuration ;
21
- public TokenController ( IOptions < DbOptions > options , IOptions < TokenAuthConfiguration > configuration )
22
+ public TokenController ( DbContext _db , IOptions < TokenAuthConfiguration > configuration )
22
23
{
23
- this . _options = options . Value ;
24
24
_configuration = configuration ;
25
+ db = _db ;
25
26
}
26
- [ HttpPost ( "/token" ) ]
27
- public IActionResult Create ( string username , string password )
27
+ [ HttpGet ( "/token" ) ]
28
+ public IActionResult Create ( TokenInput input )
28
29
{
29
30
JObject ht = new JObject ( ) ;
30
31
ht . Add ( "code" , "200" ) ;
31
32
ht . Add ( "msg" , "success" ) ;
32
- if ( username != password )
33
+ var us = db . LoginDb . GetSingle ( it => it . userName == input . username ) ;
34
+ if ( us == null )
33
35
{
34
-
36
+ ht [ "code" ] = "201" ;
37
+ ht [ "msg" ] = "用户名或者密码错误!" ;
38
+ return Ok ( ht ) ;
39
+ }
40
+ string str = SimpleStringCipher . Instance . Encrypt ( input . password , null , Encoding . ASCII . GetBytes ( us . passWordSalt ) ) ;
41
+ if ( ! us . passWord . Equals ( str ) )
42
+ {
43
+ ht [ "code" ] = "201" ;
44
+ ht [ "msg" ] = "用户名或者密码错误!" ;
45
+ return Ok ( ht ) ;
35
46
}
36
-
37
47
var identity = new ClaimsIdentity ( ) ;
38
- identity . AddClaim ( new Claim ( ClaimTypes . NameIdentifier , "1" ) ) ;
39
- identity . AddClaim ( new Claim ( ClaimTypes . Name , "1" ) ) ;
40
- identity . AddClaim ( new Claim ( ClaimTypes . Role , "" ) ) ;
41
- identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Sub , username ) ) ;
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 ) ) ;
42
51
identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Jti , Guid . NewGuid ( ) . ToString ( ) ) ) ;
43
52
identity . AddClaim ( new Claim ( JwtRegisteredClaimNames . Iat , DateTimeOffset . Now . ToUnixTimeSeconds ( ) . ToString ( ) , ClaimValueTypes . Integer64 ) ) ;
44
53
@@ -69,6 +78,11 @@ private string CreateAccessToken(IEnumerable<Claim> claims, TimeSpan? expiration
69
78
return new JwtSecurityTokenHandler ( ) . WriteToken ( jwtSecurityToken ) ;
70
79
}
71
80
}
81
+ public class TokenInput
82
+ {
83
+ public string username { get ; set ; }
84
+ public string password { get ; set ; }
85
+ }
72
86
public class AuthenticateResultModel
73
87
{
74
88
public string AccessToken { get ; set ; }
0 commit comments