I have an ASP.NET Core 6.0 project and I use 2 authentication schemes.
One of the authencation schemes is, as default "JwtBearerDefaults.AuthenticationScheme"
and this works only for Role-based auth.
another one is "Custom"
"Custom" is used only for Policy-based auth.
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
.....
}
.AddJwtBearer("Custom", options =>
{
.....
});
And after that I added some scopes to "Custom" policy.
I would like to reach the controller with;
[Authorize(AuthenticationSchemes ="Custom",Policy = "RandomPolicyName")]
OR
[Authorize(Roles="User")]
I know I cannot use both on the same controller, it cannot work because it has to pass both authorize attributes. So how can I do that ??
[Authorize(Policy = "RandomPolicyName", Roles = "User")]? How do you implements policy based? The common way should be like this answer. Pls share more code for how do you implements jwt.