Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Mvc 6 get user claims immediately after log in

In a Asp.net Mvc 6 website I customized the out-of-the-box AccountController so that I add a custom Claim before calling userManager.CreateAsync(), both when registering

  • with a local account (email/password), in the Register() method of the controller
  • with an external provider (google/facebook), in the ExternalLoginConfirmation() of the controller

Now what I'd like to do is, immediately after the user logs in, both

  • in the Login() method after calling signInManager.PasswordSignInAsync() and
  • in the ExternalLoginCallback() method after calling signInManager.ExternalLoginSignInAsync()

...to retrieve that claim, ideally without hitting the DB to get the User.

I noticed that if I look at User directly after sign-in, the Claims collection is empty. However, if I look at it in a subsequent controller action the Claims collection is populated and has my custom claim in it.

The question is, why isn't the Claims populated immediately after sign-in (I guess the sign-in code doesn't refresh the CurrentPrincipal?) and is there another place to check directly after sign-in to get the claims without hitting the DB?

like image 704
adrian h. Avatar asked Nov 09 '22 00:11

adrian h.


1 Answers

You could try to implement your own ApplicationUserStore and fill whatever properties of the user (or entity which is used as identity entity) you need.

like image 51
Dawid Rutkowski Avatar answered Nov 14 '22 21:11

Dawid Rutkowski