Base solution for your next web application
Open Closed

Account->Login Async database action (User could not login) #7055


User avatar
0
carelearning created

Dear Support,

We are using the legacy MVC 5 version. In the Account controller and Login action we would like to run a SQL query (ClearUserLocksAsync) against a custom entity when an user logins in. However this returns the "The user could not login" error. We can wrap it in Try/Catch and ignore the exception. But we would like to explore if there is a better alternative.

Account->Login
      [HttpPost]
      [UnitOfWork]
      public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
      {
          // ... unmodified
          await UnitOfWorkManager.Current.SaveChangesAsync();

          // Custom call
          await _accessAppService.ClearUserLocksAsync(loginResult.User.Id);

          return Json(new AjaxResponse { TargetUrl = returnUrl });
      }
    }
    

AccessService->ClearUserLocksAsync

     public async Task ClearUserLocksAsync(long id)
    {
        var userLocks = await _lockRepository
            .GetAll()
            .Where(x => x.UserId == id)
            .ToListAsync();

        foreach (var userLock in userLocks)
            await _lockRepository.DeleteAsync(userLock);
    }
    

Error

Current user did not login to the application!

Stack Trace

Abp.Authorization.AbpAuthorizationException: Current user did not login to the application! at Abp.Authorization.AuthorizationHelper.<AuthorizeAsync>d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Authorization.AuthorizationHelper.<CheckPermissions>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Authorization.AuthorizationHelper.<AuthorizeAsync>d__16.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.Run(Func`1 action) at Abp.Authorization.AuthorizationHelperExtensions.Authorize(IAuthorizationHelper authorizationHelper, MethodInfo methodInfo, Type type) at Abp.Authorization.AuthorizationInterceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Auditing.AuditingInterceptor.PerformAsyncAuditing(IInvocation invocation, AuditInfo auditInfo) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.AccessAppServiceProxy.ClearUserLocksAsync(Int64 id) at MyCompanyName.AbpZeroTemplate.Web.Controllers.AccountController.


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

    Can you share error logs and stack information?

  • User Avatar
    0
    carelearning created

    Dear Support, It has been 9 days since we have last heard from you. We added our stack information when requested and was just checking in to see if there is any update on this or if anymore information is needed. Thank you,

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @carelearning

    Sorry for our late response. Could you add [DisableAuditing] attribute to your ClearUserLocksAsync method in AccessService and try again ?

    Thanks,

  • User Avatar
    0
    maliming created
    Support Team

    Sorry, didn't reply in time, please reply to the new comment directly next time, so we can receive email notification.

    Because the Login method has not been executed and returned, there is no real login success.

    The simplest solution is to call the ClearUserLocksAsync method again after the login is successful.

    Or you can set the current TenantId and UserId to skip verification (not recommended).

    [HttpPost]
    [UnitOfWork]
    public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
    {
      // ... unmodified
      await UnitOfWorkManager.Current.SaveChangesAsync();
    
    	using (AbpSession.Use(loginResult.Tenant?.Id, loginResult.User.Id)))
    	{
    		// Custom call
    		await _accessAppService.ClearUserLocksAsync(loginResult.User.Id);
    	}
    
      return Json(new AjaxResponse { TargetUrl = returnUrl });
    }