Open Closed

API authorisation failure for microservice using MongoDB #2705


User avatar
0
vincent.goh created
  • ABP Framework version: v4.4.4 Commercial
  • UI type: Blazor WebAssembly
  • DB provider: MongoDB
  • Tiered (MVC) or Identity Server Separated (Angular): Microservice

I followed this guide to create a new microservice that uses MongoDB -> https://support.abp.io/QA/Questions/1269/using-MongoDB-in-microservice-service-inside-microservice-solution-which-uses-efcore

However, the new microservice will occasionally throw up an "Authorization failed! Given policy has not granted." error when an API endpoint is called. The same API endpoint will normally work fine so it is not due to user permission settings. The issue will only occur some time after using the application and while the user is still logged in.

I have tried my best to figure out the cause and replicate the problem, but this issue will only happen after the application has been working fine for a couple of hours so it's difficult to troubleshoot and identify why it suddenly stopped working. I've tried running the same application on different machines, networks and environments and the issue is still there. Restarting the application, OS and physical machine doesn't make the issue go away. However, after a few hours, the application will work normally again. After a while, the problem will come back. It is frustrating and I have run out of things to try.

Hope you have some suggestions for me to try. If it helps, I can put up the source code on Github for you to take a look.

The error log can be found below.

[21:55:54 INF] Request starting HTTP/1.1 GET https://localhost:44363/api/i-t-service/alarms - 0 [21:55:55 INF] Successfully validated the token. [21:55:55 INF] Executing endpoint 'NaviX.UnifiedDigitalPlatform.ITService.Controllers.Alarms.AlarmController.GetListAsync (NaviX.UnifiedDigitalPlatform.ITService.HttpApi)' [21:55:55 INF] Route matched with {area = "iTService", controller = "Alarm", action = "GetList"}. Executing controller action with signature System.Threading.Tasks.Task1[Volo.Abp.Application.Dtos.PagedResultDto1[NaviX.UnifiedDigitalPlatform.ITService.Alarms.AlarmDto]] GetListAsync(NaviX.UnifiedDigitalPlatform.ITService.Alarms.GetAlarmsInput) on controller NaviX.UnifiedDigitalPlatform.ITService.Controllers.Alarms.AlarmController (NaviX.UnifiedDigitalPlatform.ITService.HttpApi). [21:55:55 DBG] PermissionStore.GetCacheItemAsync: pn:U,pk:e50d88b7-5179-744b-f4c9-39fe5509621d,n:ITService.Alarms [21:55:55 DBG] Found in the cache: pn:U,pk:e50d88b7-5179-744b-f4c9-39fe5509621d,n:ITService.Alarms [21:55:55 DBG] PermissionStore.GetCacheItemAsync: pn:R,pk:admin,n:ITService.Alarms [21:55:55 DBG] Found in the cache: pn:R,pk:admin,n:ITService.Alarms [21:55:55 DBG] PermissionStore.GetCacheItemAsync: pn:C,pk:WebGateway_Swagger,n:ITService.Alarms [21:55:55 DBG] Found in the cache: pn:C,pk:WebGateway_Swagger,n:ITService.Alarms [21:55:55 INF] Authorization failed. These requirements were not met: PermissionRequirement: ITService.Alarms [21:55:55 WRN] ---------- RemoteServiceErrorInfo ---------- { "code": "Volo.Authorization:010001", "message": "Authorization failed! Given policy has not granted.", "details": null, "data": {}, "validationErrors": null } [21:55:55 WRN] Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown. Volo.Abp.Authorization.AbpAuthorizationException: Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown. at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.CheckAsync(IAuthorizationService authorizationService, AuthorizationPolicy policy) at Volo.Abp.Authorization.MethodInvocationAuthorizationService.CheckAsync(MethodInvocationAuthorizationContext context) at Volo.Abp.Authorization.AuthorizationInterceptor.AuthorizeAsync(IAbpMethodInvocation invocation) at Volo.Abp.Authorization.AuthorizationInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Auditing.AuditingInterceptor.ProceedByLoggingAsync(IAbpMethodInvocation invocation, IAuditingHelper auditingHelper, IAuditLogScope auditLogScope) at Volo.Abp.Auditing.AuditingInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at lambda_method1503(Closure , Object ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) [21:55:55 WRN] Code:Volo.Authorization:010001 [21:55:55 INF] Executing ObjectResult, writing value of type 'Volo.Abp.Http.RemoteServiceErrorResponse'. [21:55:55 INF] Executed action NaviX.UnifiedDigitalPlatform.ITService.Controllers.Alarms.AlarmController.GetListAsync (NaviX.UnifiedDigitalPlatform.ITService.HttpApi) in 322.2834ms [21:55:55 INF] Executed endpoint 'NaviX.UnifiedDigitalPlatform.ITService.Controllers.Alarms.AlarmController.GetListAsync (NaviX.UnifiedDigitalPlatform.ITService.HttpApi)' [21:55:55 INF] Request finished HTTP/1.1 GET https://localhost:44363/api/i-t-service/alarms - 0 - 403 - application/json;+charset=utf-8 630.2044ms


11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Please share the project with me, I will check it. shiwei.liang@volosoft.com

  • User Avatar
    0
    vincent.goh created

    I have invited you to a Github repository (vincentgoh478/NaviX.UnifiedDigitalPlatform.ITService).

    I've only included the source code for the specific microservice that is using MongoDB and is having issues. If you need to access the entire codebase of the microservice solution, let me know and I can add the source code in.

    Thanks.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I can't build the project you provided

  • User Avatar
    0
    vincent.goh created

    I have added all the solutions to the repository so you should be able to build and test out the ITService microservice now.

    Let me know if you need anything else. Thanks!

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I tried many times, it always work for me.

  • User Avatar
    0
    vincent.goh created

    Yes, it will build and run fine initially but after a few hours, the error will occur only for ITService and not the other services. Try leaving the services running in the background for a few hours and you should encounter the errors on ITService.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Ok, I will give it a try

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    These dependencies should not be added. you need to remove these.

    We have an example of a microservice that uses both EF Core and MongoDB, you can refer it: https://github.com/abpframework/eShopOnAbp/tree/main/services/catalog/src

  • User Avatar
    0
    vincent.goh created

    Many thanks! Let me try this out and see if it resolves the issue. Will update soon.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @vincent.goh, have you tried it?

  • User Avatar
    0
    vincent.goh created

    Yes, seems to be working fine now without any issues. Thanks very much for the help!

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11