Owin-Authorization icon indicating copy to clipboard operation
Owin-Authorization copied to clipboard

Gets an exception Microsoft.Owin.Security.Authorization.WebApi.ResourceAuthorizeAttribute.IsAuthorized

Open sid1260 opened this issue 3 years ago • 1 comments

Hi David, I have a .NetFramework 4.7 WebApi project. I had added the Microsoft.Owin.Security.Authorization.WebApi Nuget package.

In my StartUp,cs I have the following line app.UseAuthorization(o => { o.AddPolicy("contribute", policy => policy.RequireClaim(JwtClaimTypes.Role, "User.Contributor")); });

In my controller I have added [HttpGet] [Route("{cargoShipmentGBID}/parties")] [ResourceAuthorize(Policy = "contribute")] [SwaggerResponse(HttpStatusCode.OK)] [SwaggerResponse(HttpStatusCode.BadRequest,"Bad request")] [SwaggerResponse(HttpStatusCode.NotFound,"Could not find a booking based on the ID")] [SwaggerOperation(OperationId = "Get Booking Parties")]
public async Task<HttpResponseMessage> RetrievePartyDetails(int cargoShipmentGBID)

However I end up with the following exception

{ "Message": "An error has occurred.", "ExceptionMessage": "One or more errors occurred.", "ExceptionType": "System.AggregateException", "StackTrace": " at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)\r\n at System.Threading.Tasks.Task1.get_Result()\r\n at Microsoft.Owin.Security.Authorization.WebApi.ResourceAuthorizeAttribute.IsAuthorized(HttpActionContext actionContext)\r\n at System.Web.Http.AuthorizeAttribute.OnAuthorization(HttpActionContext actionContext)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()", "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Value cannot be null.\r\nParameter name: context", "ExceptionType": "System.ArgumentNullException", "StackTrace": " at Microsoft.Owin.Security.Authorization.OwinContextExtensions.GetAuthorizationOptions(IOwinContext context)\r\n at Microsoft.Owin.Security.Authorization.AuthorizationHelper.<IsAuthorizedAsync>d__2.MoveNext()" } }

Do you have any idea?

Thanks,

sid1260 avatar Jun 14 '22 09:06 sid1260

It is likely throwing on this line: https://github.com/DavidParks8/Owin-Authorization/blob/38f2dd771193506d2c497e4deaf83483976ba464/src/Microsoft.Owin.Security.Authorization/AuthorizationHelper.cs#L88

That would only happen if the code was not running with the proper owin dependencies.

DavidParks8 avatar Jun 30 '22 06:06 DavidParks8