In ASP.NET Core 6.0 project, I have a requirement to authenticate the request if the authorization bearer token satisfies any of the authentication schema say "jwt1" or "jwt2".
The authorization policy also differ based on the authentication schema that was applied to authenticate the request. This is because the claim to validate vary based on the authentication schema that was applied to authenticate the request.
If the request is authenticated via schema say "jwt1" then I want to apply the authorization policy "jwt1_policy"
[Authorize(Policy="jwt1_policy")]
[Route("weather"), HttpGet]
If the request is authenticated via schema say "jwt2" then I want to apply the authorization policy "jwt2_policy" instead of "jwt1_policy" on the same endpoint.
[Authorize(Policy="jwt2_policy")]
[Route("weather"), HttpGet]
I want to apply either "jwt1_policy" or "jwt2_policy" on the same endpoint depending on the authentication schema that was applied to authenticate the request.
As I understand from the document, if I add multiple authorization attributes in the same endpoint then both policies will be checked.
[Authorize(Policy="jwt1_policy")]
[Authorize(Policy="jwt2_policy")]
[Route("weather"), HttpGet]
Please let me know if anyone has idea of how to choose different authorization policy based on the authentication schema on the same endpoint.
I also have a related question on the RequiredScope attribute.
Is it possible to apply the RequiredScope only if a particular authentication schema was applied. We want to apply the RequiredScope only if authentication schema jwt1 was applied to authenticate the request. Is that possible to achieve? Is it possible to implement custom version of RequiredScope attribute so that we can skip the RequiredScope check if a different authentication schema was applied to authenticate the request?
[Authorize]
[RequiredScope("read")
[Route("weather"), HttpGet]
You could create 3 policy to allow either of the scheme. For example
Then
[Authorize(Policy = "Jwt_Or_Cookie")]will work for either authentication.[Authorize(Policy = "Only_Jwt")]will only allow jwt authentication.[Authorize(Policy = Only_Cookie")]will only allow cookie authentication.