Code: Select all
public class PermissionAuthorizationPolicyProvider : DefaultAuthorizationPolicyProvider
{
public PermissionAuthorizationPolicyProvider(IOptions options)
: base(options) { }
///
public override async Task GetPolicyAsync(string policyName)
{
// ...
return new AuthorizationPolicyBuilder().AddRequirements(requirement).Build();
}
}
Code: Select all
public class PermissionHandler : AuthorizationHandler
{
private readonly ICurrentTenantService _currentTenantService;
private readonly ISecurityService _securityService;
public PermissionHandler(ICurrentTenantService currentTenantService, ISecurityService securityService)
{
_currentTenantService = currentTenantService;
_securityService = securityService;
}
protected override async Task HandleRequirementAsync(
AuthorizationHandlerContext context, PermissionRequirement requirement)
{
// Authorizing.....
}
}
< /code>
und sie sind so registriert: < /p>
builder.Services.AddScoped(); // contain connStr
builder.Services.AddDbContext();
builder.Services.AddDbContext();
// Register custom Authorization handler
builder.Services.AddScoped();
// Overrides the DefaultAuthorizationPolicyProvider
builder.Services.AddSingleton();
// Middlewares
app.UseMiddleware();
app.UseAuthorize();