AccountController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. spDbContext dbContext;
  24. public AccountController(IConfiguration configuration,spDbContext _dbContext)
  25. {
  26. Configuration = configuration;
  27. dbContext = _dbContext;
  28. }
  29. [HttpGet,HttpPost,Route("Login")]
  30. public userToken Login(loginDto loginUser)
  31. {
  32. //添加验证代码
  33. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Account == loginUser.Name && s.Password == utility.MD5Utility.GetMD5(loginUser.Password)).FirstOrDefault();
  34. if(staff != null)
  35. {
  36. return GetToken(staff);
  37. }
  38. else
  39. {
  40. return new userToken() {
  41. StatusCode = System.Net.HttpStatusCode.NotFound,
  42. Name = null,
  43. Token = null
  44. };
  45. }
  46. }
  47. [HttpGet,Route("GetUser")]
  48. public userToken GetUser()
  49. {
  50. if (User.Identity.IsAuthenticated)//如果Token有效
  51. {
  52. var name = User.Claims.First(x => x.Type == ClaimTypes.Name).Value;//从Token中拿出用户ID
  53. //模拟获得Token
  54. spDbContext dbContext = new spDbContext();
  55. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Account == name).FirstOrDefault();
  56. if (staff != null)
  57. {
  58. return GetToken(staff);
  59. }
  60. else
  61. {
  62. return new userToken() {StatusCode=System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  63. }
  64. }
  65. else
  66. {
  67. return new userToken() {StatusCode= System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  68. }
  69. }
  70. private userToken GetToken(Staff staff)
  71. {
  72. var claims = new List<Claim>()
  73. {
  74. new Claim("Id",staff.Id.ToString()),
  75. new Claim(ClaimTypes.Name,staff.Name),
  76. new Claim(ClaimTypes.Email,(staff.Mail==null)?"":staff.Mail),
  77. };
  78. var positions = dbContext.DepartmentPositions.Where<DepartmentPosition>(dp => dp.StaffId == staff.Id).ToList();
  79. foreach (var p in positions)
  80. {
  81. claims.Add(new Claim(ClaimTypes.Role, $"[{p.departmentId}]-[{p.PositionId}]"));
  82. }
  83. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"]));
  84. var credential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  85. var expireTime = DateTime.Now.AddMinutes(20);
  86. var token = new JwtSecurityToken(
  87. issuer: Configuration["jwt:Issuer"],
  88. audience: Configuration["jwt:Audience"],
  89. claims: claims.ToArray(),
  90. notBefore: DateTime.Now,
  91. expires: expireTime,
  92. signingCredentials: credential);
  93. string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
  94. //建立UserToken物件後回傳client
  95. userToken userToken = new userToken()
  96. {
  97. StatusCode = System.Net.HttpStatusCode.OK,
  98. UserId = staff.Id,
  99. Name = staff.Name,
  100. Token = jwtToken,
  101. ExpireTime = expireTime
  102. };
  103. return userToken;
  104. }
  105. [HttpGet, HttpPost, Route("GetRoles")]
  106. public List<string> GetRoles(string ResourceId)
  107. {
  108. if(ResourceId == "AddProjectJX")
  109. {
  110. return new List<string> { "[35]-[2]" };
  111. }
  112. return new List<string>();
  113. }
  114. }
  115. }