User.Identity.GetUserId() returns null after successful login

后端 未结 9 2534
傲寒
傲寒 2020-12-05 09:52

I\'ve defined a temp variable to get current user id, it always returns null.

Here is the snapshot:

Why?

UPDATE:

         


        
相关标签:
9条回答
  • 2020-12-05 10:13

    I get the user doing the following right after sign-in:

    var userId = SignInManager.AuthenticationManager.AuthenticationResponseGrant.Identity.GetUserId();
    var user = SignInManager.UserManager.Users.Where(x => x.Id.Equals(userId)).FirstOrDefault();
    
    0 讨论(0)
  • 2020-12-05 10:17

    Simply try this :

    string userId = SignInManager
    .AuthenticationManager
    .AuthenticationResponseGrant.Identity.GetUserId();
    
    0 讨论(0)
  • 2020-12-05 10:19

    Here's what worked for me:

    await SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: false);
    
    AuthenticationManager.User = new GenericPrincipal(AuthenticationManager.AuthenticationResponseGrant.Identity, null);
    

    Once executed, you get authenticated state for the current request.

    0 讨论(0)
提交回复
热议问题