Base solution for your next web application
Open Closed

Setting Property value of Custom Session at anywhere #3820


User avatar
0
fastsoft created

Hi!

I created custom session class. And ı can call it in my services. i can get session property value when i set add claim at TokenAuthController.cs>Authenticate methot.(loginResult.Identity.AddClaim(new Claim("Application_ActiveSchoolName", "High Trade School"))).

But i can't get value when i set value in any service methot.

FastAppSession.cs

public class FastAppSession : ClaimsAbpSession, ITransientDependency
    {
        public FastAppSession(
        IPrincipalAccessor principalAccessor,
        IMultiTenancyConfig multiTenancy,
        ITenantResolver tenantResolver,
        IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) :
        base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider)
        {

        }

        public string ActiveSchoolName
        {
            get
            {
                var activeSchoolName = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "Application_ActiveSchoolName");
                if (string.IsNullOrEmpty(activeSchoolName?.Value))
                {
                    return null;
                }

                return activeSchoolName.Value;
            }
            set
            {
                ((ClaimsIdentity)PrincipalAccessor.Principal?.Identity).AddClaim(new Claim("Application_ActiveSchoolName", value));
            }
        }
    }

OgrenciAppService.cs

public class OgrenciAppService : OKYSFastAppServiceBase, IOgrenciAppService
    {
        private IRepository<VOgrenciInfo> _ogrenciRepository;

        public OgrenciAppService() : base()
        {
            _ogrenciRepository = new Repository<VOgrenciInfo>();
        }
        //Methods
        [HttpPost]
        public async Task<PagedResultDto<VOgrenciInfo>> GetData(FGetEntityExpression searchParams)
        {

            string test = FastAppSession.ActiveSchoolName; //it return null every calling

            FastAppSession.ActiveSchoolName = "High Trade School";

            test = FastAppSession.ActiveSchoolName; //it return setting value one time.

            return await _ogrenciRepository.GetDataWithPaging(searchParams);
        }
}

1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @fastsoft,

    When setting the claim, you are adding a new claim but when getting it, you are getting the first item. When setting the claim, you should change or remove existing claim with the same key.

    Thanks.