ResourceOwnerPasswordValidator.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using IdentityServer4.Models;
  2. using IdentityServer4.Validation;
  3. using QM.AuthServer.IRepository;
  4. using QM.AuthServer.Models.Users;
  5. using System.Net.Http;
  6. using System.Security.Claims;
  7. using System.Threading.Tasks;
  8. using WS;
  9. using WS.Helper;
  10. namespace QM.AuthServer.Auth
  11. {
  12. /// <summary>
  13. /// 资源所有者密码验证程序
  14. /// </summary>
  15. public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
  16. {
  17. private IAuthorizationRepository authorizationRepository;
  18. private ISysUserRepository _ISysUserRepository;
  19. public ResourceOwnerPasswordValidator(
  20. IAuthorizationRepository _authorizationRepository,
  21. ISysUserRepository ISysUserRepository
  22. )
  23. {
  24. this.authorizationRepository = _authorizationRepository;
  25. this._ISysUserRepository = ISysUserRepository;
  26. }
  27. public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
  28. {
  29. SysUser loginUser = null;
  30. if (context.Password == "@AUTH@")
  31. {
  32. loginUser = _ISysUserRepository.Get(t=>t.uid == context.UserName);
  33. //同步
  34. if(loginUser == null)
  35. {
  36. }
  37. }
  38. else
  39. {
  40. loginUser = authorizationRepository.Login(new LoginDto() { Id = context.UserName, Pwd = context.Password });
  41. }
  42. if (loginUser == null)
  43. {
  44. context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Invalid client credential");
  45. }
  46. else
  47. {
  48. context.Result = new GrantValidationResult(
  49. subject: context.UserName,
  50. authenticationMethod: "custom",
  51. claims: new Claim[] {
  52. new Claim("UserId", loginUser.uid),
  53. new Claim("UserName", loginUser.name),
  54. new Claim("UserImg", loginUser.img)
  55. }
  56. );
  57. }
  58. }
  59. public async Task ValidateAsync2(ResourceOwnerPasswordValidationContext context)
  60. {
  61. SysUser loginUser = null;
  62. var mode = ConfigHelper.Configuration["Auth:Mode"];
  63. if(mode.ToUpper() == "PT")
  64. {
  65. var url = ConfigHelper.Configuration["Auth:PT_Ticket"];
  66. using (HttpClient http = new HttpClient())
  67. {
  68. var result = await http.GetAsync(url + context.Password);
  69. if (result.IsSuccessStatusCode)
  70. {
  71. string msg = await result.Content.ReadAsStringAsync();
  72. var res = msg.ToModel<PtResult>();
  73. if (res.code == 200)
  74. loginUser = new SysUser() { uid = res.data.userInfo.NO, name = res.data.userInfo.NAME };
  75. }
  76. }
  77. }
  78. else
  79. {
  80. loginUser = authorizationRepository.Login(new LoginDto() { Id = context.UserName, Pwd = context.Password });
  81. }
  82. if (loginUser == null)
  83. {
  84. context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Invalid client credential");
  85. }
  86. else
  87. {
  88. context.Result = new GrantValidationResult(
  89. subject: context.UserName,
  90. authenticationMethod: "custom",
  91. claims: new Claim[] {
  92. new Claim("UserId", loginUser.uid),
  93. new Claim("UserName", loginUser.name),
  94. new Claim("UserImg", loginUser.img)
  95. }
  96. );
  97. }
  98. }
  99. }
  100. }