Closed gorums closed 2 years ago
Hello @gorums,
To my knowledge, the resource is always going to be null in the handler when you are using the ResourceAuthorizeAttribute.
Alternatively, your issue can be solved by replacing your ResourceAuthorizeAttribute in favor of IAuthorizationService. The service can be injected in the controller. Doing so will let you specify anything (including the current HttpContext) to the resource parameter in the AuthorizeAsync method.
According to the doc, the official ASP.NET Core version will let you access the MVC context in the handler when using the AuthorizeAttribute. It does not seem to work in the backport however.
I branched the code locally and worked a fix that will automatically assign the current HttpActionContext for WebAPI or the AuthorizationContext for MVC to the resource when using the ResourceAuthorizeAttribute. However, I would like to discuss the idea with the author before submitting a pull request.
@DavidParks8, let me know if that makes sense or if I am mistaken. I can create a pull request if the proposition is in line with your vision.
Thank you fore reading me through.
Thanks @lavignep007. I injected a HttpContextWrapper to obtain the HttpContext as you suggest and worked, but should be good idea get HttpContext from AuthorizationHandlerContext too.
@gorums, I'm glad it worked.
I have forked the code for now, you will find what you are missing here: https://github.com/lavignep007/Owin-Authorization/tree/contextinhandler
I will submit a pull request if the author is OK with it, until then, you will have to build and host the resulting package yourself for now.
Please submit the PR! I have no issues with making the library better no matter who does it.
Awesome, I just did!
Dear @lavignep007 ,
Thanks for the answer. Could you please expand on this?
Alternatively, your issue can be solved by replacing your ResourceAuthorizeAttribute in favor of IAuthorizationService. The service can be injected in the controller. Doing so will let you specify anything (including the current HttpContext) to the resource parameter in the AuthorizeAsync method.
I am quite new to .NET Framework and making my way through a legacy system. I am not sure how to inject the service in the controller and define policies on it.