Browse Source

#5523 Get current user claims from AuthenticationStateProvider

pull/5922/head
Halil İbrahim Kalkan 6 years ago
parent
commit
9310db485b
  1. 29
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Security/AbpWebAssemblyClaimsCache.cs
  2. 72
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentPrincipalAccessor.cs
  3. 13
      framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/AbpHttpClientIdentityModelWebAssemblyModule.cs

29
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Security/AbpWebAssemblyClaimsCache.cs

@ -0,0 +1,29 @@
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Security
{
[ExposeServices(
typeof(AbpWebAssemblyClaimsCache),
typeof(IAsyncInitialize)
)]
public class AbpWebAssemblyClaimsCache : ISingletonDependency, IAsyncInitialize
{
public ClaimsPrincipal Principal { get; private set; }
private readonly AuthenticationStateProvider _authenticationStateProvider;
public AbpWebAssemblyClaimsCache(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}
public virtual async Task InitializeAsync()
{
var authenticationState = await _authenticationStateProvider.GetAuthenticationStateAsync();
Principal = authenticationState.User;
}
}
}

72
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentPrincipalAccessor.cs

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using Volo.Abp.AspNetCore.Mvc.Client;
using System.Security.Claims;
using Volo.Abp.AspNetCore.Components.WebAssembly.Security;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims;
@ -9,75 +7,17 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly
{
public class WebAssemblyCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITransientDependency
{
protected ICachedApplicationConfigurationClient ConfigurationClient { get; }
protected AbpWebAssemblyClaimsCache ClaimsCache { get; }
public WebAssemblyCurrentPrincipalAccessor(
ICachedApplicationConfigurationClient configurationClient)
AbpWebAssemblyClaimsCache claimsCache)
{
ConfigurationClient = configurationClient;
ClaimsCache = claimsCache;
}
protected override ClaimsPrincipal GetClaimsPrincipal()
{
var configuration = ConfigurationClient.Get();
var claims = new List<Claim>();
if (configuration.CurrentUser.Id != null)
{
claims.Add(new Claim(AbpClaimTypes.UserId,configuration.CurrentUser.Id.ToString()));
}
if (!configuration.CurrentUser.UserName.IsNullOrWhiteSpace())
{
claims.Add(new Claim(AbpClaimTypes.UserName,configuration.CurrentUser.UserName));
}
if (!configuration.CurrentUser.Email.IsNullOrWhiteSpace())
{
claims.Add(new Claim(AbpClaimTypes.Email,configuration.CurrentUser.Email));
}
if (configuration.CurrentUser.EmailVerified)
{
claims.Add(new Claim(AbpClaimTypes.EmailVerified,"true"));
}
if (!configuration.CurrentUser.Name.IsNullOrWhiteSpace())
{
claims.Add(new Claim(AbpClaimTypes.Name,configuration.CurrentUser.Name));
}
if (!configuration.CurrentUser.PhoneNumber.IsNullOrEmpty())
{
claims.Add(new Claim(AbpClaimTypes.PhoneNumber,configuration.CurrentUser.PhoneNumber));
}
if (configuration.CurrentUser.PhoneNumberVerified)
{
claims.Add(new Claim(AbpClaimTypes.PhoneNumberVerified,"true"));
}
if (!configuration.CurrentUser.SurName.IsNullOrWhiteSpace())
{
claims.Add(new Claim(AbpClaimTypes.SurName,configuration.CurrentUser.SurName));
}
if (configuration.CurrentUser.TenantId != null)
{
claims.Add(new Claim(AbpClaimTypes.TenantId,configuration.CurrentUser.TenantId.ToString()));
}
else if (configuration.CurrentTenant.Id != null)
{
claims.Add(new Claim(AbpClaimTypes.TenantId,configuration.CurrentTenant.Id.ToString()));
}
foreach (var role in configuration.CurrentUser.Roles)
{
claims.Add(new Claim(AbpClaimTypes.Role, role));
}
return new ClaimsPrincipal(new ClaimsIdentity(claims));
return ClaimsCache.Principal;
}
}
}

13
framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/AbpHttpClientIdentityModelWebAssemblyModule.cs

@ -1,5 +1,6 @@
using System;
using IdentityModel;
using Volo.Abp.Modularity;
using Volo.Abp.Security.Claims;
namespace Volo.Abp.Http.Client.IdentityModel.WebAssembly
{
@ -8,6 +9,14 @@ namespace Volo.Abp.Http.Client.IdentityModel.WebAssembly
)]
public class AbpHttpClientIdentityModelWebAssemblyModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
AbpClaimTypes.UserName = JwtClaimTypes.PreferredUserName;
//AbpClaimTypes.Name = ...; //TODO
//AbpClaimTypes.SurName = ...; //TODO
AbpClaimTypes.UserId = JwtClaimTypes.Subject;
AbpClaimTypes.Role = JwtClaimTypes.Role;
AbpClaimTypes.Email = JwtClaimTypes.Email;
}
}
}

Loading…
Cancel
Save