SessionService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.AspNetCore.Identity;
  2. using System;
  3. using System.Linq;
  4. using VCommon.Security;
  5. using VCommon.VApplication.Authorization;
  6. using VCommon.VApplication.Security;
  7. using VCommon.VOpenApi.VAspNetCore;
  8. using VCommonCoreExample.AppServices.Basic;
  9. using VCommonCoreExample.AppServices.Session.Dto;
  10. using VCommonCoreExample.Authorization;
  11. using VCommonCoreExample.Caching;
  12. using VCommonCoreExample.Caching.Models;
  13. using VCommonCoreExample.DataStore;
  14. using VCommonCoreExample.Entity;
  15. namespace VCommonCoreExample.AppServices.Session
  16. {
  17. public interface ISessionService
  18. {
  19. string Login(SessionLoginInput input);
  20. [VServiceAuthorize]
  21. SessionOutput GetSession();
  22. [VServiceAuthorize]
  23. void Logout();
  24. }
  25. public class SessionService : DbAppServiceBase, ISessionService
  26. {
  27. private readonly UserStore _userStore;
  28. private readonly SessionCache _sessionCache;
  29. private readonly PermissionProvider _permissionProvider;
  30. public SessionService(UserStore userStore, SessionCache sessionCache, PermissionProvider permissionProvider)
  31. {
  32. _userStore = userStore;
  33. _sessionCache = sessionCache;
  34. _permissionProvider = permissionProvider;
  35. }
  36. public string Login(SessionLoginInput input)
  37. {
  38. if (VSession.UserId.HasValue) throw new VFriendlyException("您已登录,要更换用户,请退出登录");
  39. var delayer = new Delayer(immediatelyStart: true);
  40. var usr = _userStore.GetLoginInfo(input.TenantCode, input.LoginName, out var message);
  41. if (null == usr)
  42. {
  43. delayer.StopAndDelay();
  44. throw new VFriendlyException(message ?? "登录名错误");
  45. }
  46. var match = new PasswordHasher<User>().VerifyHashedPassword(null, usr.Password, input.Password);
  47. if (PasswordVerificationResult.Failed == match)
  48. {
  49. delayer.StopAndDelay();
  50. throw new VFriendlyException("登录名和密码不匹配");
  51. }
  52. if (false == usr.IsEnable)
  53. {
  54. delayer.StopAndDelay();
  55. throw new VFriendlyException("用户未启用");
  56. }
  57. var token = RandomStringGenerator.GuidBasedRandomHexString64();
  58. _sessionCache.Set(token, new SessionCacheModel
  59. {
  60. TenantId = usr.TenantId,
  61. UserId = usr.Id,
  62. HashedPassword = usr.Password,
  63. Remember = input.Remember ?? false,
  64. KickSign = Guid.NewGuid().ToString()
  65. });
  66. return token;
  67. }
  68. public void Logout()
  69. {
  70. _sessionCache.Clear(VSession.Token);
  71. }
  72. public SessionOutput GetSession()
  73. {
  74. var uid = VSession.GetUserId();
  75. var tid = VSession.TenantId;
  76. using var db = GetDbContext();
  77. var usr = db.Users
  78. .Where(p => p.IsAbolish == false && p.TenantId == tid && p.Id == uid)
  79. .Select(p => new { p.Name, p.Roles }).FirstOrDefault();
  80. if (usr == null) throw new VFriendlyException("找不到用户");
  81. return new SessionOutput
  82. {
  83. TenantId = tid,
  84. UserId = uid,
  85. UserName = usr.Name,
  86. Permissions = _permissionProvider.GetUserPermissionCodes(VSession.TenantId, VSession.GetUserId()),
  87. //Setting = new Dictionary<string, object>(), //TODO: UserProfileService Setting
  88. };
  89. }
  90. }
  91. }