I am developing a Web API and using the Microsoft.Identity.Web libraries for securing the APIs.
I have a scenario where different APIs / Controllers need to accept tokens issued by different Azure AD App Registrations
At the moment, I have something like this:
services.AddMicrosoftIdentityWebApiAuthentication(Configuration.GetSection("Api1"));
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.Build();
});
...
//Controller1
[Authorize]
[ApiController]
public class Controller1 : ControllerBase
{...}
In the example Above, I use Api1 Configuration section to provide a ClientID/Tenant/Audience values for my Azure AD App registration.
I would like to be able to add another Authorization "rule" (?) in a way that I can configure Controller2 to accept tokens from a second App Registration:
services.AddMicrosoftIdentityWebApiAuthentication(Configuration.GetSection("Api1"));
services.AddMicrosoftIdentityWebApiAuthentication(Configuration.GetSection("Api2")); //this probably won't work as it will clobber the services instance?
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.Build();
});
...
//Controller1
[Authorize] --- With Api1 settings?
[ApiController]
public class Controller1 : ControllerBase
{...}
//Controller2
[Authorize] --- With Api2 settings?
[ApiController]
public class Controller2 : ControllerBase
{...}
The requirements of why I need to use 2 different app registrations are outside of my scope and cannot change.
Currently, I solved this by creating 2 Web API projects / apps but it's come to a point where I really like to consolidate these if possible.