Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to update Claims #8173


User avatar
0
maharatha created

I am using the below atricle to implement Organization filter :

https://aspnetboilerplate.com/Pages/Documents/Articles\How-To\add-custom-data-filter-ef-core

Everything works fine, but I would like to know how to update the user's claim.


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi maharatha

    You can refer to this issue https://github.com/aspnetboilerplate/aspnetboilerplate/issues/5092#issuecomment-562006265

  • User Avatar
    0
    maharatha created

    This is the approach i am taking :

    var userOuClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "Application_OrganizationUnitId");
    
                if (AbpSession.UserId.HasValue && AbpSession.TenantId.HasValue)
                {
                    var currentUserOrgId =
                        _cacheManager.GetCache(PSLConsts.UserOrganizationCache)
                            .GetOrDefault(AbpSession.UserId.Value.ToString() + AbpSession.TenantId.Value);
                    if (currentUserOrgId != null)
                    {
                        return Convert.ToInt32(currentUserOrgId);
                    }
    
                    if (string.IsNullOrEmpty(userOuClaim?.Value)) return null;
                    _cacheManager.GetCache(PSLConsts.UserOrganizationCache).Set(AbpSession.UserId.Value.ToString(), userOuClaim.Value);
                    return Convert.ToInt32(userOuClaim.Value);
    
                }
    
                if (AbpSession.UserId.HasValue && !AbpSession.TenantId.HasValue)
                {
    
                    var currentUserOrgId =
                        _cacheManager.GetCache(PSLConsts.UserOrganizationCache)
                            .GetOrDefault(AbpSession.UserId.Value.ToString());
    
                    if (currentUserOrgId != null)
                    {
                        return Convert.ToInt32(currentUserOrgId);
                    }
    
                    if (string.IsNullOrEmpty(userOuClaim?.Value)) return null;
                    _cacheManager.GetCache(PSLConsts.UserOrganizationCache).Set(AbpSession.UserId.Value.ToString(), userOuClaim.Value);
                    return Convert.ToInt32(userOuClaim.Value);
    
    
                }
    

    I am somehow not convinced with the elegence of the solution. Is there a better way. The idea is to set the Orgid when the user changes the Org or fall back to Default orgID. This is applicable for Host and Tenant

  • User Avatar
    0
    maliming created
    Support Team

    I don't think you should use PrincipalAccessor but use cache directly.

    Use TenantId and UserId as cache keys. var cacheKey = $"{AbpSession.TenantId ?? 0}_{AbpSession.UserId.Value.ToString()}";

    Read userOu from cache just like custom session. Update the cache when userOu changes.

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because it has not had recent activity for a long time.