Open Closed

Volo.Abp.Authorization.AbpAuthorizationException and Background Job #2232


User avatar
0
Rrader30 created
  • ABP Framework version: v4.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace: Volo.Abp.Authorization.AbpAuthorizationException
  • Steps to reproduce the issue:"

I created a background job. I generated a class in the *.Domain project.

My class is defined below

public class PassiveCheckerWorker : AsyncPeriodicBackgroundWorkerBase
{

    public PassiveCheckerWorker(AbpAsyncTimer timer,
                                IServiceScopeFactory serviceScopeFactory) : base(timer,serviceScopeFactory)
    {
        Timer.Period = 150000; //600000; //10 minutes
    }

    protected async override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
    {
        Logger.LogInformation("Starting: Setting status of inactive users...");

        var _objectMapper = workerContext.ServiceProvider.GetRequiredService<IObjectMapper>();

        GetContractorsInput _input = new GetContractorsInput();
        var _contractor = workerContext.ServiceProvider.GetRequiredService<IContractorsAppService>(); //IRepository<Contractor, Guid>>(); //IContractorsAppService>();

        //IQueryable<Contractor> queryable = await _contractor.GetQueryableAsync();

        DateTime _today = DateTime.Now;
        DateTime _monthAgo = _today.AddMonths(-1);

        //Set WCI Expiration
        _input.wciexpirationdateMin = _monthAgo;
        _input.wciexpirationdateMax = _today;

        PagedResultDto<ContractorDto> _clist = await _contractor.GetListAsync(_input);

        //var query = from contractor in queryable
        //            where contractor.wciexpirationdate <= _today
        //            orderby contractor.Id
        //            select contractor;

        //var _cntr = query.ToList();

        foreach (var _c in _clist.Items)
        {
            //Check status
            if (_c.status != 5 && _c.status != 6 && _c.status != 3)
            {
                //Change status
                _c.status = 6;
                ContractorUpdateDto _update = new ContractorUpdateDto();
                var _cntr = _objectMapper.Map<ContractorDto, ContractorUpdateDto>(_c);
                await _contractor.UpdateAsync(_c.Id, _cntr);

                //TODO: Send out email notification to vendor
            }
        }

        //Set GLI Expiration
        GetContractorsInput _input2 = new GetContractorsInput();
        _input2.gliexperationdateMin = _monthAgo;
        _input2.gliexperationdateMax = _today;

        PagedResultDto<ContractorDto> _clist2 = await _contractor.GetListAsync(_input2);
        foreach (var _c in _clist2.Items)
        {
            //Check status
            if (_c.status != 5 && _c.status != 6 && _c.status != 3)
            {
                //Change status
                _c.status = 5;
                ContractorUpdateDto _update = new ContractorUpdateDto();
                var _cntr = _objectMapper.Map<ContractorDto, ContractorUpdateDto>(_c);
                await _contractor.UpdateAsync(_c.Id, _cntr);

                //TODO: Send out email notification to vendor
            }
        }

        Logger.LogInformation("Completed: Setting status of inactive users...");
    }
}

I registed it in the DomainModule.cs file

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    context.AddBackgroundWorker<PassiveCheckerWorker>();
}

the background job executes fine. My Issue is I get the following in the Audit log

               [
  {
    "code": "Volo.Authorization:010001",
    "message": "Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown.",
    "details": "AbpAuthorizationException: Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown.\r\nSTACK TRACE:    at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.CheckAsync(IAuthorizationService authorizationService, AuthorizationPolicy policy)\r\n   at Volo.Abp.Authorization.MethodInvocationAuthorizationService.CheckAsync(MethodInvocationAuthorizationContext context)\r\n   at Volo.Abp.Authorization.AuthorizationInterceptor.AuthorizeAsync(IAbpMethodInvocation invocation)\r\n   at Volo.Abp.Authorization.AuthorizationInterceptor.InterceptAsync(IAbpMethodInvocation invocation)\r\n   at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)\r\n   at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)\r\n   at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()\r\n   at Volo.Abp.GlobalFeatures.GlobalFeatureInterceptor.InterceptAsync(IAbpMethodInvocation invocation)\r\n   at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)\r\n   at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)\r\n   at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()\r\n   at Volo.Abp.Auditing.AuditingInterceptor.ProceedByLoggingAsync(IAbpMethodInvocation invocation, IAuditingHelper auditingHelper, IAuditLogScope auditLogScope)\r\n   at Volo.Abp.Auditing.AuditingInterceptor.ProcessWithNewAuditingScopeAsync(IAbpMethodInvocation invocation, AbpAuditingOptions options, ICurrentUser currentUser, IAuditingManager auditingManager, IAuditingHelper auditingHelper)\r\n",
    "data": null,
    "validationErrors": null
  }
]

I have also attempted to run a linq query and by-pass the application layer, but my code seems to hang on

var query = from contractor in queryable
                   where contractor.wciexpirationdate <= _today
                    orderby contractor.Id
                       select contractor;

  var _cntr = query.ToList();

How can I execute a background job as a user or how can I by-pass the authentication?


1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    https://support.abp.io/QA/Questions/485/Background-Worker-needs-the-authorization

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15