[RESOLVED]Get Custom Identity Field and store it into Session as soon as the website loads.

Hello! I do not know how to write this. I have added a custom field/column to the AspNetUsers table, which I use to load a custom ID after the user comes back to the site and is authenticated.

So, what I’m trying to do right now is to load it and store it into a session as soon as the website loads, independently from the controller which is called.

The problem that I’m facing right now is that I do not see an Identity method that actually grabs the data I want.

So, what I’m trying to do is to launch this code as soon as I can.

   var manager = new UserManager<AppUserModels>(new UserStore<AppUserModels>(new AppIdentityDbContext()));
   var currentUser = manager.FindById(User.Identity.GetUserId());
   Session["ID"] = currentUser.BoId.BoId;

But I don’t know where and how. I’m doing this because I do not want to call it from each controller.


Why don’t you issue it as a claim so it’s in the cookie? Look at the API that converts the User object into claims in the sample code. This is a much better approach and you don’t have to use session state (which you should avoid).

Thanks! I haven’t had the chance to use claims yet!

So should I avoid using Session[] at all?


So should I avoid using Session[] at all?


"Session state is stored in-memory by default. This is a horrible option if you want to remember data for your users. In ASP.NET the hosting application pool

frequently recycles
and this is typically out of your control. This means that all that in-memory state is lost, which is why using session state in-proc is a bad choice."


Thanks a lot!


I’d like to thank you for the amazing page you’ve linked! I’m also working with something related to a shopping cart, and it was great to know these practices. It makes lots of sense what is written in the article. I shall use cookies instead of session
from now on.

Now, I’d like to also take this chance to learn how do I read the claim. I don’t know how. Just learned how to set the default like this:

  public class DefaultIdentityFactory : ClaimsIdentityFactory<AppUserModels, string>
            public override async Task<ClaimsIdentity> CreateAsync(UserManager<AppUserModels, string> userManager,
                                        AppUserModels user, string authType)
                var claimsIdentity = await base.CreateAsync(userManager, user, authType);
                claimsIdentity.AddClaim(new Claim("ID", user.IboID.IboID.ToString()));
                return claimsIdentity; ;

And then, I would call it in here:

   public AppUserManager(IUserStore<AppUserModels> store)
            : base(store)
            this.ClaimsIdentityFactory = new DefaultIdentityFactory();

Everything, so far, so good.

But I do not know which method is the one which takes care of calling the Claim’s value. 

Thanks a million!

var cp = (ClaimsPrincipal)User;

var someClaim = cp.FindFirst("someCLaimType");

var someValue = someClaim.Value;

Oooofffffff. Freaking sweeeeeeeettttt! This makes me super excited!!! :D :D :D

You’re the man! Cool Works flawlessly!!!! And that has solved a problem I’ve been fighting with for over a week!!!!! !YESSSSSS!!!!!!

That did the job! A  million thank you!!!

Woohoo – wohooooo hahahaha!!!!

Edit: Hahaha. I didn’t realize that it was your website ;).

Leave a Reply