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

Change Claim #4784


User avatar
0
kasem created

Hi

I use ASP.NET Core with Angular version and I implemented a custom session (used MyAppSession example suggested by you), and I'm able to add a custom property and get it anywhere in the app. The problem that I need to allow user change this Claim value in the app, and when I try to do this, Claim is changed in that Request context, but not persisted over further requests and I still get the initial value set in Login. I understand that I should ReLogin user after changing Claim. Can you please provide me the needed steps using "MyAppSession" example for ASP.NET Core version (with .NET Framework)? I do prefer a better approach like Refreshing Identity if that's an option.

Second question: MyAppSession is injected and used in a custom data filter in AppDbContext, but it doesn't resolve the Claim Value there (Hardcoded value works in that filter) seems some LifeCycle related problem?

Appreciate your assistance solving these issues


11 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    how about using Settings for the user. <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Setting-Management">https://aspnetboilerplate.com/Pages/Doc ... Management</a>

  • User Avatar
    0
    kasem created

    Thank you. I agree, it's easier to handle this through configurations rather than a Claim

  • User Avatar
    0
    alper created
    Support Team

    yeah it's pretty much straight forward to use settings. you can reach it from anywhere. plus it persists the data ;)

  • User Avatar
    0
    kasem created

    Hi Alper

    How can I use User setting value in the custom filter I added? It seems this filter runs once in app bootstrap for all objects that implement my interface, when AbpSession object is null (no UserId nor TenantId).

    Would be great if you can provide a sample

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi Kasem,

    You can check the current filters here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.EntityFrameworkCore/EntityFrameworkCore/AbpDbContext.cs">https://github.com/aspnetboilerplate/as ... Context.cs</a>.

  • User Avatar
    0
    kasem created

    Hi Ismcagdas

    The problem that filter gets the initial value available in that object which in turn resolves it from settings, but when that setting changed, filter still get the initial value and never changed.

    Please note that I didn't follow exactly what you did (didn't implement null pattern), should I do so? do you believe it should fix the problem? If yes, can you please provide an amended version for your code here (I get that setting value from a property in that custom session object): <a class="postlink" href="https://gist.github.com/ismcagdas/6f2a9a3b5d7b907cb8d94a72124d59a1">https://gist.github.com/ismcagdas/6f2a9 ... 72124d59a1</a>

    Thanks

  • User Avatar
    0
    alper created
    Support Team

    don't use property for setting filter value. if you do that it'll not be updated after you change your setting value. better create a method that returns setting value.

    bool IsEnabled(){
     //return setting value
    }
    
  • User Avatar
    0
    kasem created

    Thanks Alper for your quick response

    I tried a method too, and it still get the initial value. I even added a breakpoint to that method to see if it's called when I reach the query that should apply the filter, but it's not reached.

    it's reached only once, when app is bootstrapped but not after

  • User Avatar
    0
    alper created
    Support Team

    In AbpDbContext class, add your custom session property

    public IMyCustomSession MyCusomSession { get; set; }
    protected virtual int MyValue  => GetMyValue();
    protected virtual int GetMyValue()
    {
    	return MyCustomSession .MyValue;
    }
    
  • User Avatar
    0
    kasem created

    That really works! Many thanks Alper!

  • User Avatar
    0
    alper created
    Support Team

    congrats ;)