locked
UserManager value is null for first time to the function FindByNameAsync() RRS feed

  • Question

  • User1389134248 posted

    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
    var user = await userManager.FindByNameAsync(context.UserName);

    I have got the value in context.UserName and the same username exists in DB. But FindByNameAsync() function returning null sometimes for the first call.

    if I call the same function again it is giving output as expected. when I call it as in the below code.

    if(user == null) {

    var user = await userManager.FindByNameAsync(context.UserName);

    }

    Can anyone help me out of this?

    Many thanks in advance.

    Note: It's not happening every time but happening quite often.

    Monday, August 12, 2019 6:32 AM

All replies

  • User475983607 posted

    After a successful login the username is located in the current ClaimsPrincipal.

    string user = User.Identity.Name;

    Monday, August 12, 2019 10:12 AM
  • User-719153870 posted

    Hi bbratnam,

    Commonly, we get Username like below:

    string username = Context.User.Identity.GetUserName();

    Or:

    string username = Context.User.Identity.Name;

    With above code, it's hard to tell where is the problem.

    Here i'll provide some examples about FindByNameAsync() you can refer to: FindByNameAsync().

    Best Regard,

    Yang Shen

    Tuesday, August 13, 2019 3:16 AM