| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.Linq;
- using System.Security.Claims;
- using System.Security.Principal;
- using Abp.Extensions;
- using JetBrains.Annotations;
- namespace Abp.Runtime.Security
- {
- public static class ClaimsIdentityExtensions
- {
- public static UserIdentifier GetUserIdentifierOrNull(this IIdentity identity)
- {
- Check.NotNull(identity, nameof(identity));
- var userId = identity.GetUserId();
- if (userId == null)
- {
- return null;
- }
- return new UserIdentifier(identity.GetTenantId(), userId.Value);
- }
- public static long? GetUserId([NotNull] this IIdentity identity)
- {
- Check.NotNull(identity, nameof(identity));
- var claimsIdentity = identity as ClaimsIdentity;
- var userIdOrNull = claimsIdentity?.Claims?.FirstOrDefault(c => c.Type == AbpClaimTypes.UserId);
- if (userIdOrNull == null || userIdOrNull.Value.IsNullOrWhiteSpace())
- {
- return null;
- }
- return Convert.ToInt64(userIdOrNull.Value);
- }
- public static int? GetTenantId(this IIdentity identity)
- {
- Check.NotNull(identity, nameof(identity));
- var claimsIdentity = identity as ClaimsIdentity;
- var tenantIdOrNull = claimsIdentity?.Claims?.FirstOrDefault(c => c.Type == AbpClaimTypes.TenantId);
- if (tenantIdOrNull == null || tenantIdOrNull.Value.IsNullOrWhiteSpace())
- {
- return null;
- }
- return Convert.ToInt32(tenantIdOrNull.Value);
- }
- public static long? GetImpersonatorUserId(this IIdentity identity)
- {
- Check.NotNull(identity, nameof(identity));
- var claimsIdentity = identity as ClaimsIdentity;
- var userIdOrNull = claimsIdentity?.Claims?.FirstOrDefault(c => c.Type == AbpClaimTypes.ImpersonatorUserId);
- if (userIdOrNull == null || userIdOrNull.Value.IsNullOrWhiteSpace())
- {
- return null;
- }
- return Convert.ToInt64(userIdOrNull.Value);
- }
- public static int? GetImpersonatorTenantId(this IIdentity identity)
- {
- Check.NotNull(identity, nameof(identity));
- var claimsIdentity = identity as ClaimsIdentity;
- var tenantIdOrNull = claimsIdentity?.Claims?.FirstOrDefault(c => c.Type == AbpClaimTypes.ImpersonatorTenantId);
- if (tenantIdOrNull == null || tenantIdOrNull.Value.IsNullOrWhiteSpace())
- {
- return null;
- }
- return Convert.ToInt32(tenantIdOrNull.Value);
- }
- }
- }
|