-
Notifications
You must be signed in to change notification settings - Fork 272
authorization policies
Jean-Marc Prieur edited this page Oct 29, 2021
·
13 revisions
You have several ways of specifying that scopes are required to call a web API:
- using the
RequiredScopeson a controller, or a controller action - defining authentication policies at the level of your application, and enforcing them for the app, or on any
[Authorize]attribute (using the Policy= property of that attribute)
The RequiredScopes have two exclusive parameters:
- scopes (passed directly in the constructor of the attribute
- configuration entry pointing to scopes. You use the
RequiredScopesConfigurationKeyproperty.
This code snippet expresses that the controller requires
[RequiredScopes("access_as_user")]
public class MyController : ApiController
{
}[RequiredScope(RequiredScopesConfigurationKey="AzureAd:Scopes")]
public class MyController : ApiController
{
}public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(...)
/// ...
policy="MyPolicy";
services.AddAuthorization(options =>
{
options.AddPolicy(policy, policyBuilder => {
policyBuilder.Requirements.Add(new ScopeAuthorizationRequirement() { RequiredScopesConfigurationKey = $"{ConfigSectionName}:Scope" });
});
}); policy="MyPolicy";
services.AddAuthorization(options =>
{
options.AddPolicy(policy, policyBuilder => {
policyBuilder.RequireScope(scopes?.Split(' '));
});
});and then on the controller or controller action:
[Authorize(Policy="MyPolicy")]
public class MyController : ApiController
{
}If you choose to have the policy as a default policy, you don't need to specify the policy in the Authorize attribute.
policy="MyPolicy";
services.AddAuthorization(options =>
{
options.AddPolicy(policy, policyBuilder => {
policyBuilder.RequireScope(scopes?.Split(' '));
});
builder.DefaultPolicy = builder.GetPolicy(policyName);
[Authorize]
public class MyController : ApiController
{
}The recommendation is to use the ASP.NET Core authorization policies. The following code snippet shows how to ensure that all the request are filtered for users belonging to particular tenants.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(...)
/// ...
// To add authorization
services.AddAuthorization(builder =>
{
string policyName = "user belongs to specific tenant";
string[] allowedTenants =
{
"14c2f153-90a7-4689-9db7-9543bf084dad",
"af8cc1a0-d2aa-4ca7-b829-00d361edb652",
"979f4440-75dc-4664-b2e1-2cafa0ac67d1",
};
builder.AddPolicy(policyName, b => {
b.RequireClaim("http://schemas.microsoft.com/identity/claims/tenantid",
allowedTenants);
});
builder.DefaultPolicy = builder.GetPolicy(policyName);
});
}- Home
- Why use Microsoft Identity Web?
- Web apps
- Web APIs
- Minimal support for .NET FW Classic
- Logging
- Azure AD B2C limitations
- Samples
- Certificates
- Managed Identity as Federated Credential
- Federated Credentials from other Identity Provider
- Extensibility: Bring your own credential
- Get client secrets from KeyVault
- Web apps
- Web app samples
- Web app template
- Call an API from a web app
- Managing incremental consent and conditional access
- Web app troubleshooting
- Deploy to App Services Linux containers or with proxies
- SameSite cookies
- Hybrid SPA
- Web APIs
- Web API samples
- Web API template
- Call an API from a web API
- Token Decryption
- Web API troubleshooting
- web API protected by ACLs instead of app roles
- gRPC apps
- Azure Functions
- Long running processes in web APIs
- Authorization policies
- Generic API
- Customization
- Logging
- Calling graph with specific scopes/tenant
- Multiple Authentication Schemes
- Utility classes
- Setting FIC+MSI
- Mixing web app and web API
- Deploying to Azure App Services
- Azure AD B2C issuer claim support
- Performance
- specify Microsoft Graph scopes and app-permissions
- Integrate with Azure App Services authentication
- Ajax calls and incremental consent and conditional access
- Back channel proxys
- Client capabilities