AccountController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, HttpPost, Route("ChangePassword")]
  48. public bool ChangePassword(changePasswordDto dto)
  49. {
  50. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Id == dto.UserId && s.Password == utility.MD5Utility.GetMD5(dto.oldPassword)).FirstOrDefault();
  51. if (staff != null)
  52. {
  53. staff.Password = utility.MD5Utility.GetMD5(dto.newPassword);
  54. dbContext.SaveChanges();
  55. return true;
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. }
  62. [HttpGet,Route("GetUser")]
  63. public userToken GetUser()
  64. {
  65. if (User.Identity.IsAuthenticated)//如果Token有效
  66. {
  67. var name = User.Claims.First(x => x.Type == ClaimTypes.Name).Value;//从Token中拿出用户ID
  68. //模拟获得Token
  69. spDbContext dbContext = new spDbContext();
  70. Staff staff = dbContext.Staffs.Where<Staff>(s => s.Account == name).FirstOrDefault();
  71. if (staff != null)
  72. {
  73. return GetToken(staff);
  74. }
  75. else
  76. {
  77. return new userToken() {StatusCode=System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  78. }
  79. }
  80. else
  81. {
  82. return new userToken() {StatusCode= System.Net.HttpStatusCode.NotFound, Name = null, Token = null };
  83. }
  84. }
  85. private userToken GetToken(Staff staff)
  86. {
  87. var claims = new List<Claim>()
  88. {
  89. new Claim("Id",staff.Id.ToString()),
  90. new Claim(ClaimTypes.Name,staff.Name),
  91. new Claim(ClaimTypes.Email,(staff.Mail==null)?"":staff.Mail),
  92. };
  93. var positions = dbContext.DepartmentPositions.Where<DepartmentPosition>(dp => dp.StaffId == staff.Id).ToList();
  94. foreach (var p in positions)
  95. {
  96. claims.Add(new Claim(ClaimTypes.Role, $"[{p.departmentId}]-[{p.PositionId}]"));
  97. }
  98. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["jwt:Key"]));
  99. var credential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  100. var expireTime = DateTime.Now.AddMinutes(20);
  101. var token = new JwtSecurityToken(
  102. issuer: Configuration["jwt:Issuer"],
  103. audience: Configuration["jwt:Audience"],
  104. claims: claims.ToArray(),
  105. notBefore: DateTime.Now,
  106. expires: expireTime,
  107. signingCredentials: credential);
  108. string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
  109. //建立UserToken物件後回傳client
  110. userToken userToken = new userToken()
  111. {
  112. StatusCode = System.Net.HttpStatusCode.OK,
  113. UserId = staff.Id,
  114. Name = staff.Name,
  115. Token = jwtToken,
  116. ExpireTime = expireTime
  117. };
  118. return userToken;
  119. }
  120. [HttpGet, HttpPost, Route("GetRoles")]
  121. public List<string> GetRoles(string ResourceId)
  122. {
  123. if(ResourceId == "AddProjectJX")
  124. {
  125. return new List<string> { "[35]-[2]" };
  126. }
  127. return new List<string>();
  128. }
  129. }
  130. }