mirror of https://github.com/abpframework/abp.git
9 changed files with 168 additions and 5 deletions
@ -0,0 +1,18 @@ |
|||
using System.Security.Claims; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.Security.Claims; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Security.Claims |
|||
{ |
|||
public class HttpContextCurrentPrincipalAccessor : ThreadCurrentPrincipalAccessor |
|||
{ |
|||
public override ClaimsPrincipal Principal => _httpContextAccessor.HttpContext?.User ?? base.Principal; |
|||
|
|||
private readonly IHttpContextAccessor _httpContextAccessor; |
|||
|
|||
public HttpContextCurrentPrincipalAccessor(IHttpContextAccessor httpContextAccessor) |
|||
{ |
|||
_httpContextAccessor = httpContextAccessor; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System.Security.Claims; |
|||
|
|||
namespace Volo.Abp.Security.Claims |
|||
{ |
|||
/// <summary>
|
|||
/// Used to get ABP-specific claim type names.
|
|||
/// </summary>
|
|||
public static class AbpClaimTypes |
|||
{ |
|||
/// <summary>
|
|||
/// UserId.
|
|||
/// Default: <see cref="ClaimTypes.Name"/>
|
|||
/// </summary>
|
|||
public static string UserName { get; set; } = ClaimTypes.Name; |
|||
|
|||
/// <summary>
|
|||
/// UserId.
|
|||
/// Default: <see cref="ClaimTypes.NameIdentifier"/>
|
|||
/// </summary>
|
|||
public static string UserId { get; set; } = ClaimTypes.NameIdentifier; |
|||
|
|||
/// <summary>
|
|||
/// UserId.
|
|||
/// Default: <see cref="ClaimTypes.Role"/>
|
|||
/// </summary>
|
|||
public static string Role { get; set; } = ClaimTypes.Role; |
|||
|
|||
/// <summary>
|
|||
/// UserId.
|
|||
/// Default: <see cref="ClaimTypes.Email"/>
|
|||
/// </summary>
|
|||
public static string Email { get; set; } = ClaimTypes.Email; |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Security.Claims; |
|||
|
|||
namespace Volo.Abp.Security.Claims |
|||
{ |
|||
public interface ICurrentPrincipalAccessor |
|||
{ |
|||
ClaimsPrincipal Principal { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System.Security.Claims; |
|||
using System.Threading; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Security.Claims |
|||
{ |
|||
public class ThreadCurrentPrincipalAccessor : ICurrentPrincipalAccessor, ISingletonDependency |
|||
{ |
|||
public virtual ClaimsPrincipal Principal => Thread.CurrentPrincipal as ClaimsPrincipal; |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Security.Claims; |
|||
|
|||
namespace Volo.Abp.Session |
|||
{ |
|||
public class CurrentUser : ICurrentUser, ITransientDependency //TODO: Singleton?
|
|||
{ |
|||
public virtual bool IsAuthenticated => Id.HasValue; |
|||
|
|||
public virtual Guid? Id |
|||
{ |
|||
get |
|||
{ |
|||
var value = FindClaimValue(AbpClaimTypes.UserId); |
|||
if (value == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return Guid.Parse(value); |
|||
} |
|||
} |
|||
|
|||
public virtual string UserName => FindClaimValue(AbpClaimTypes.UserName); |
|||
|
|||
public virtual string Email => FindClaimValue(AbpClaimTypes.Email); |
|||
|
|||
private readonly ICurrentPrincipalAccessor _principalAccessor; |
|||
|
|||
public CurrentUser(ICurrentPrincipalAccessor principalAccessor) |
|||
{ |
|||
_principalAccessor = principalAccessor; |
|||
} |
|||
|
|||
public virtual Claim FindClaim(string claimType) |
|||
{ |
|||
return _principalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == claimType); |
|||
} |
|||
|
|||
public virtual T FindClaimValue<T>(string claimType) |
|||
where T : struct |
|||
{ |
|||
var value = FindClaimValue(claimType); |
|||
if (value == null) |
|||
{ |
|||
return default(T); |
|||
} |
|||
|
|||
return value.To<T>(); |
|||
} |
|||
|
|||
public virtual string FindClaimValue(string claimType) |
|||
{ |
|||
return FindClaim(claimType)?.Value; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Security.Claims; |
|||
|
|||
namespace Volo.Abp.Session |
|||
{ |
|||
public interface ICurrentUser |
|||
{ |
|||
bool IsAuthenticated { get; } |
|||
|
|||
Guid? Id { get; } |
|||
|
|||
string UserName { get; } |
|||
|
|||
string Email { get; } |
|||
|
|||
Claim FindClaim(string claimType); |
|||
|
|||
string FindClaimValue(string claimType); |
|||
|
|||
T FindClaimValue<T>(string claimType) |
|||
where T : struct; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue