UserContextService.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Security.Principal;
  3. using BO.AppServer.Data.Entity;
  4. using Microsoft.AspNetCore.Identity;
  5. using Microsoft.Extensions.Logging;
  6. using Quadarax.Foundation.Core.Business;
  7. namespace BO.AppServer.Business.Services.Base
  8. {
  9. public abstract class UserContextService : AbstractService
  10. {
  11. protected UserManager<IdentityUser> _manUser;
  12. protected UserRepo _repoUser;
  13. protected UserContextService(UserRepo repoUser, UserManager<IdentityUser> userManager, IPrincipal currentPrincipal, ILoggerFactory logger) : base(currentPrincipal, logger)
  14. {
  15. _manUser = userManager ?? throw new ArgumentNullException(nameof(userManager));
  16. _repoUser = repoUser ?? throw new ArgumentNullException(nameof(repoUser));
  17. }
  18. protected User GetCurrentUser()
  19. {
  20. var userIdt = _manUser.FindByNameAsync(CurrentPrincipal.Identity.Name).Result;
  21. return _repoUser.GetByIfReference(userIdt.Id);
  22. }
  23. protected long GetCurrentUserId()
  24. {
  25. var user = GetCurrentUser();
  26. return user.Id;
  27. }
  28. }
  29. }