aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Authorization Handlers Fail to run with nullable resource

Open darl0026 opened this issue 8 years ago • 3 comments

In Asp.Net Core 2, I have a handler with the following signature (the resource is a nullable type). The handler fails to run when a pass a null resource is passed into it. Im guessing this happens when you pass a null value for a ref type resource as well.

Handler Signature: internal class TreatyManagementGroupAccessRequirementHandler : AuthorizationHandler<TreatyManagementGroupAccessRequirement, int?>

Code calling handler: int? tmg = editModel.TreatyInformation.TreatyManagementGroupId;

// this code always fails and never runs the above Auth handler. var authResult = await _authorizationService.AuthorizeAsync(User, tmg, Policies.RequireEditPermission);

darl0026 avatar Apr 24 '18 20:04 darl0026

I ... oh, I don't think we've ever considered the resource would be null. @HaoK ?

blowdart avatar Apr 25 '18 02:04 blowdart

My scenario is that I have to check if the user is assigned to the treaty management group. However, we don't require that they actually select a treaty management group, thus it could be a null id.

darl0026 avatar Apr 25 '18 02:04 darl0026

That's a behavior of the default base AuthenticationHandler<TReq, TResources> (it doesn't invoke your method if resources is null) just implement IAuthenticationHandler directly and then you have full control

HaoK avatar Apr 25 '18 03:04 HaoK