Open Closed

How to add New Claims to Current Principal Thread #3990


User avatar
0
david.hurtado created
  • ABP Framework version: v4.3.3

  • UI type: Blazor Server

  • DB provider: EF Core and Dapper

  • Tiered : yes

  • Exception message and stack trace:

  • *Steps to reproduce the issue:"

  • I need your help or a guide to make this: We know that the current user has a list of claims. We need to add particular parameters or settings and new values to that claim's list. When the app needs to read a value, it could query it from the current user, i need to apply this both in blazor server(UI) and in the API (backend).

I test this code:

`public class SessionValueService : ApplicationService, ISessionValue, ITransientDependency  {
        private readonly ICurrentPrincipalAccessor _currentPrincipalAccessor;        
        public SessionValueService(ICurrentPrincipalAccessor currentPrincipalAccessor) {
            _currentPrincipalAccessor = currentPrincipalAccessor;
        }
        public async Task<string> GetSessionValueAsync(string key) {
            var ClaimObligatorio = TicketsSettings.CompaniaActualId;
            var x = _currentPrincipalAccessor.Principal.Claims.FirstOrDefault(p => p.Type.Equals(ClaimObligatorio));
            if (x != null) {
                x = _currentPrincipalAccessor.Principal.Claims.FirstOrDefault(p => p.Type.Equals(key));
                if (x != null) {
                    return x.Value;
                }
            } else {
                var claimsPrincipal = await CargarValoresGlobales();
                _currentPrincipalAccessor.Change(claimsPrincipal);
                return await GetSessionValueAsync(key);
            }
            return null;
        }`

But when the app uses this method, there is not any claim (charged or load) in the main current user thread.

Anybody could help me to do this?


4 Answer(s)
  • User Avatar
    1
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    JWT token is designed to be immutable, but you can create a middleware to change the ICurrentPrincipalAccessor to add the claims. It should work on the current HTTP request.

    But for Blazor server UI. It uses SignalR to synchronize operations, you can create a hub filter.

  • User Avatar
    0
    david.hurtado created

    Hi, @iangshiwei thanks for your answer. Do you know where i can find an example? Or Something similar?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I will make an example for you.

  • User Avatar
    1
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Backend:

    .....
    app.UseAuthorization();
    
    app.Use(async (httpContext, next) =>
    {
        var currentUser = httpContext.RequestServices.GetRequiredService<ICurrentUser>();
        var currentPrincipal = httpContext.RequestServices.GetRequiredService<ICurrentPrincipalAccessor>();
        var claimsIdentity = currentPrincipal.Principal.Identities.FirstOrDefault();
        
        if (currentUser.IsAuthenticated)
        {
            claimsIdentity?.AddClaim(new Claim("test","test"));
        }
    
        using (currentPrincipal.Change(claimsIdentity))
        {
            await next.Invoke();
        }
    });
    

    Blazor:

    public class MyHubFilter : IHubFilter
    {
    
        public virtual async ValueTask<object> InvokeMethodAsync(HubInvocationContext invocationContext,
            Func<HubInvocationContext, ValueTask<object>> next)
    
        {
            var currentUser = invocationContext.ServiceProvider.GetRequiredService<ICurrentUser>();
    
            if (!currentUser.IsAuthenticated)
            {
                return await next(invocationContext);
            }
    
            var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService<ICurrentPrincipalAccessor>();
    
            var claimsIdentity = currentPrincipalAccessor.Principal.Identities.First();
            claimsIdentity.AddClaim(new Claim("test","test"));
    
            using (currentPrincipalAccessor.Change(claimsIdentity))
            {
                return await next(invocationContext);
            }
    
        }
    }
    
    Configure<HubOptions>(options =>
    {
        options.AddFilter<MyHubFilter >();
    });
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11