Base solution for your next web application
Open Closed

Usermanager not hitting database #8303


User avatar
0
robrechtbelien created

Hi, I have a situation where directly after a login attempt (an unsuccessfull one) the usermanager doesn't get the latest values from the database. After running logInManager.LoginAsync() i see that the [AccessFailedCount] is updated in the database. But when running userManager.FindByNameAsync() in the same function after the login, the values of the returned user are the ones from before the login attempt. Is this some kind of caching somewhere? And how can I make sure to get the latest values. I'm using the .net core & MVC version .

Regards, Robrecht


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Can you share some relevant code?

  • User Avatar
    0
    robrechtbelien created
    private async Task<AbpLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName)
    {
               var loginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);
                if (loginResult.Result != AbpLoginResultType.Success)
            {
                var contact = _contactRepository.FindByUsernamePassword(usernameOrEmailAddress, password).FirstOrDefault();
                if (contact != null)
                {
    
                    var user = await _userManager.FindByNameAsync(usernameOrEmailAddress);
                }
            }
        }
    

    In the code above, the user returned at the end returns the values from befere the LoginAsync. If I look in the database after LoginAsync the values are updated.

  • User Avatar
    0
    maliming created
    Support Team

    hi robrechtbelien

    Try using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))

    private async Task<AbpLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName)
    {
    	var loginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);
    	if (loginResult.Result != AbpLoginResultType.Success)
    	{
    		using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    		{
    			var contact = _contactRepository.
    				FindByUsernamePassword(usernameOrEmailAddress, password).
    				FirstOrDefault();
    			if (contact != null)
    			{
    				var user = await _userManager.FindByNameAsync(usernameOrEmailAddress);
    			}
    			await uow.CompleteAsync();
    		}
    	}
    }
    
  • User Avatar
    0
    robrechtbelien created

    Hi maliming,

    That did the trick! Thanks!