1 readonly ILogger _logger;
2 //ITokenBuilder是用来生成Token的
3 readonly ITokenBuilder _tokenBuilder;
4 public LoginController(ITokenBuilder tokenBuilder, ILogger logger)
5 {
6 _logger = logger;
7 _tokenBuilder = tokenBuilder;
8
9 }
10 [HttpPost]
11 public IActionResult Login([FromBody]LoginModel loginModel)
12 {
13 _logger.LogInformation($"{loginModel.UserName} login!");
14 if (loginModel.UserName == "gsw" && loginModel.Password == "111111")
15 {
16 var claims = new Claim[] {
17 new Claim(ClaimTypes.Name, "gsw"),
18 new Claim(ClaimTypes.Role, "admin"),
19
20 };
21 var token = _tokenBuilder.BuildJwtToken(claims);
22 _logger.LogInformation($"{loginModel.UserName} login success,and generate token return");
23 return new JsonResult(new { Result = true, Data = token });
24 }
25 else
26 {
27 _logger.LogInformation($"{loginModel.UserName} login faile");
28 return new JsonResult(new
29 {
30 Result = false,
31 Message = "Authentication Failure"
32 });
33 }
34 }