Base solution for your next web application
Open Closed

Current User Not Logged In preventing background job from running. #11461


User avatar
0
michael.pear created

Using AZ version 11.4.0, Aspnet Core/Angular project.

I am implementing a file import using a background job, modeled after Web.Controllers.UsersControllerBase.ImportFromExcel(). While the Users import from excel, works without problem, when my implementation is failing on the "BackgroundJobManager.EnqueueAsync" call with the exception:

INFO  2023-02-07 14:50:02,585 [35   ] xusApps.Web.Controllers.ImportController - Training Content Import From Zip  by user 24@12
WARN  2023-02-07 14:50:10,901 [58   ] Abp.BackgroundJobs.BackgroundJobManager  - Current user did not login to the application!
Abp.Authorization.AbpAuthorizationException: Current user did not login to the application!
   at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(IEnumerable`1 authorizeAttributes)
   at Abp.Authorization.AuthorizationHelper.CheckPermissionsAsync(MethodInfo methodInfo, Type type)
   at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(MethodInfo methodInfo, Type type)
   at Abp.Authorization.AuthorizationInterceptor.InternalInterceptAsynchronous(IInvocation invocation)
   at Abp.BackgroundJobs.BackgroundJobManager.TryProcessJobAsync(BackgroundJobInfo jobInfo)

The call to the background job is:

                var jobId = await BackgroundJobManager.EnqueueAsync<TrainingManagerImporter, TrainingManagerImportJobArgs>(new TrainingManagerImportJobArgs
                {
                    TenantId = AbpSession.TenantId,
                    BinaryObjectId = fileObject.Id,
                    ImportType = importType,
                    User = AbpSession.ToUserIdentifier()
                    
                });

I am passing tenant Id and user Id in the background job arguments, but the exception occurs before the ExecuteAsync method in TrainingManagerImporter (which extends AsyncBackgroundJob) is called. I've tried a debug break on the entry statement, and it is never reached (similar breakpoint is reached when using the User ImportUsersToExcelJob) I can't find any place in the ImportUsersToExcel controller code where there is an explicit login as the user, and I am using the same tenant and user for that import successfully. I believe this was working before recently converting my project from single tenant to multitenant mode, and I have many other functions (other than BackgroundJobs) that are working. Also, the controller that the EnqueueAsync is done in is prefaced by an AbpMvcAuthorize, so I expect that to reach the EnqueueAsync, that the user needs to be logged in and has the permission required.

Please advise on what the source of this issue could be and whether there is other information needed to track it down.


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @michael.pear

    Is it possible to share

    1. Full stack trace for the error message
    2. source code of TrainingManagerImporter

    Thanks,

  • User Avatar
    0
    michael.pear created

    I sent the log file (with exception) and the source code to [email protected], rather than post in full here. Note I can't get any more detailed stacktrace than provided above. I can't catch the exception, as it appears to be happening in the BackgroundJob processing. The job is added to the AbpBackgroundJobs, and the exception appears to occur when the system tries to start the job, before my user code (in TrainingManagerImporter) is reached.

  • User Avatar
    0
    michael.pear created

    Response from AZ Support's review of my TrainingManagerImporter class found that I had inadvertently included an [AbpAuthorize] attribute on the class, which was the source of the problem. Removing that, and the job now runs. Goes to show how it is easy to be blind to the obvious when reviewing your own code.

    In this process, I also refactored to make sure that no ApplicationServices (which require authorized users) were iinjected, so I switched to using domain services for the database functions. I saw that this could be a problem in other postings, such as StackOverflow https://stackoverflow.com/questions/52250802/background-job-fails-with-current-user-did-not-login-to-the-application