AccountController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.IdentityModel.Tokens;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.IdentityModel.Tokens.Jwt;
  9. using System.Linq;
  10. using System.Security.Claims;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using wispro.sp.entity;
  14. using wispro.sp.share.webViewObject;
  15. namespace wispro.sp.api.Controllers
  16. {
  17. [Route("api/[controller]")]
  18. [ApiController]
  19. public class AccountController : ControllerBase
  20. {
  21. private static List<Staff> CacheList = new List<Staff>();
  22. public IConfiguration Configuration { get; }
  23. public AccountController(IConfiguration configuration)
  24. {
  25. Configuration = configuration;
  26. }
  27. [HttpGet,HttpPost,Route("Login")]
  28. public userToken Login(loginDto loginUser)
  29. {
  30. //添加验证代码
  31. spDbContext dbContext = new spDbContext();
  32. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Account == loginUser.Name && s.Password == loginUser.Password).FirstOrDefault();
  33. if(staff != null)
  34. {
  35. return GetToken(staff);
  36. }
  37. else
  38. {
  39. return new userToken() {
  40. StatusCode = System.Net.HttpStatusCode.NotFound,
  41. Name = null,
  42. Token = null
  43. };
  44. }
  45. }
  46. [HttpGet,Route("GetUser")]
  47. public userToken GetUser()
  48. {
  49. if (User.Identity.IsAuthenticated)//如果Token有效
  50. {
  51. var name = User.Claims.First(x => x.Type == ClaimTypes.Name).Value;//从Token中拿出用户ID
  52. //模拟获得Token
  53. spDbContext dbContext = new spDbContext();
  54. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Account == name).FirstOrDefault();
  55. if (staff != null)
  56. {
  57. return GetToken(staff);
  58. }
  59. else
  60. {
  61. return new userToken() {StatusCode=System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  62. }
  63. }
  64. else
  65. {
  66. return new userToken() {StatusCode= System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  67. }
  68. }
  69. private userToken GetToken(Staff staff)
  70. {
  71. var claims = new Claim[]
  72. {
  73. new Claim("Id",staff.Id.ToString()),
  74. new Claim(ClaimTypes.Name,staff.Name),
  75. new Claim(ClaimTypes.Email,(staff.Mail==null)?"":staff.Mail),
  76. //new Claim(ClaimTypes.Role,"Admin"),
  77. };
  78. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"]));
  79. var credential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  80. var expireTime = DateTime.Now.AddMinutes(20);
  81. var token = new JwtSecurityToken(
  82. issuer: Configuration["jwt:Issuer"],
  83. audience: Configuration["jwt:Audience"],
  84. claims: claims,
  85. notBefore: DateTime.Now,
  86. expires: expireTime,
  87. signingCredentials: credential);
  88. string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
  89. //建立UserToken物件後回傳client
  90. userToken userToken = new userToken()
  91. {
  92. StatusCode = System.Net.HttpStatusCode.OK,
  93. Name = staff.Name,
  94. Token = jwtToken,
  95. ExpireTime = expireTime
  96. };
  97. return userToken;
  98. }
  99. }
  100. }