Permalink
Branch: master
Find file Copy path
682d845 Dec 17, 2018
1 contributor

Users who have contributed to this file

39 lines (32 sloc) 1.33 KB
// Copyright (c) 2018 Inventory Innovations, Inc.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace RolesToPermission
{
//thanks to https://www.jerriepelser.com/blog/creating-dynamic-authorization-policies-aspnet-core/
public class AuthorizationPolicyProvider : DefaultAuthorizationPolicyProvider
{
private readonly AuthorizationOptions _options;
private readonly IConfiguration _configuration;
public AuthorizationPolicyProvider(IOptions<AuthorizationOptions> options, IConfiguration configuration) : base(options)
{
_options = options.Value;
_configuration = configuration;
}
public override async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
{
var policy = await base.GetPolicyAsync(policyName);
if (policy == null)
{
policy = new AuthorizationPolicyBuilder()
.AddRequirements(new PermissionRequirement(policyName))
.Build();
// Add policy to the AuthorizationOptions, so we don't have to re-create it each time
_options.AddPolicy(policyName, policy);
}
return policy;
}
}
}