I want different controller methods authenticated by different JWT.
Startup.cs looks like:
services
.AddAuthentication()
.AddJwtBearer("Schema1", options =>
{
...
// use JWT Authentication with secretKey1
var issuerSecretKey = "secretKey1";
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(issuerSecretKey));
})
.AddJwtBearer("Schema2", options =>
{
...
// use JWT Authentication with secretKey2
var issuerSecretKey = "secretKey2";
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(issuerSecretKey));
});
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes("Schema1", "Schema2")
.Build();
});
Controller
[Authorize(AuthenticationSchemes = "Schema1")]
public ActionResult Method1(int id)
{
//some code
}
[Authorize(AuthenticationSchemes = "Schema2")]
public ActionResult Method2(int id)
{
//some code
}
After that I take Postman and execute the request to Method1 with JWT by secretKey2, but it successfully passes authorization! I used this answer https://stackoverflow.com/a/49706390/11593189
What should I do to authorizing Method1 using JWT by secretKey1, and authorizing Method2 using JWT by secretKey2? Maybe I should use additional mechanisms, such as Policy or Role?