IUserContentCore.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Claims;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XYY.Common.Standard;
  8. using System.Linq;
  9. namespace XYY.Authentication.Standard
  10. {
  11. public static class UserContentCoreUnit
  12. {
  13. public static UserContent GetUserContent(this ClaimsPrincipal principal)
  14. {
  15. /*
  16. new Claim(ClaimTypes.NameIdentifier,user.Id.ToString()),
  17. new Claim(ClaimTypes.Name,user.NiceName),
  18. new Claim(ClaimTypes.GroupSid,user.CustomerId.ToString()),
  19. new Claim(ClaimTypes.Role,string.Join(",",user.Roles)),
  20. new Claim(ClaimTypes.AuthorizationDecision,string.Join(",",user.PrivilegeUrl))
  21. */
  22. UserContent userContent = new UserContent();
  23. try
  24. {
  25. userContent.Id = int.Parse(principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value ?? "0");
  26. userContent.CustomerId = int.Parse(principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.GroupSid)?.Value ?? "0");
  27. userContent.NiceName = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;
  28. userContent.IsAuthorization = true;
  29. userContent.Roles = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value.Split(',').ToList();
  30. userContent.PrivilegeUrl = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.AuthorizationDecision)?.Value.Split(',').ToList();
  31. }
  32. catch (Exception ex)
  33. {
  34. userContent.IsAuthorization = false;
  35. }
  36. return userContent;
  37. }
  38. }
  39. }