123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using Ropin.Inspection.Common.Accessor.Interface;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Claims;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Common.Accessor
- {
- public class ClaimsAccessor : IClaimsAccessor
- {
- protected IPrincipalAccessor PrincipalAccessor { get; }
- public ClaimsAccessor(IPrincipalAccessor principalAccessor)
- {
- PrincipalAccessor = principalAccessor;
- }
- /// <summary>
- /// 登录用户ID
- /// </summary>
- public Guid ApiUserId
- {
- get
- {
- var userId = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
- if (userId != null)
- {
- return Guid.Parse(userId);
- }else
- {
- throw new Exception("登录用户ID不存在");
- }
- //return Guid.Empty;
- }
- }
- /// <summary>
- /// 登录用户名称
- /// </summary>
- public string ApiUserName
- {
- get
- {
- var userName = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
- if (string.IsNullOrWhiteSpace(userName))
- {
- return string.Empty;
- }
- return userName;
- }
- }
- /// <summary>
- /// 用户角色Id
- /// </summary>
- public string RoleIds
- {
- get
- {
- var roleIds = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;
- if (string.IsNullOrWhiteSpace(roleIds))
- {
- return string.Empty;
- }
- return roleIds;
- }
- }
- /// <summary>
- /// 组织架构ID
- /// </summary>
- public Guid OrganizeId
- {
- get
- {
- var primaryGroupSid = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.PrimaryGroupSid)?.Value;
- if (!string.IsNullOrEmpty(primaryGroupSid) )
- {
- return Guid.Parse(primaryGroupSid);
- }
- else
- {
- throw new Exception("组织架构不存在");
- }
- }
- }
- /// <summary>
- /// LinsenceCode
- /// </summary>
- public string Linsence
- {
- get
- {
- var primaryLinsence = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Rsa)?.Value;
- if (string.IsNullOrEmpty(primaryLinsence))
- {
- //throw new Exception("LinsenceCode不存在");
- return null;
- }
- else
- {
- return primaryLinsence;
- }
-
- }
- }
- /// <summary>
- /// LicenseTypeCode
- /// </summary>
- public string LicenseTypeCode
- {
- get
- {
- var licenseTypeCode = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid)?.Value;
- if (string.IsNullOrEmpty(licenseTypeCode))
- {
- //throw new Exception("LicenseTypeCode不存在");
- return null;
- }
- else
- {
- return licenseTypeCode;
- }
-
- }
- }
- /// <summary>
- /// 用户组织架构类型
- /// </summary>
- public string OrgTypeCode
- {
- get
- {
- var orgTypeCode = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Actor)?.Value;
- if (string.IsNullOrEmpty(orgTypeCode))
- {
- throw new Exception("组织架构类型不存在");
- }
- else
- {
- return orgTypeCode;
- }
- }
- }
-
- }
- }
|