In my ASP.NET Core MVC app, I have a class that inherits from AuthorizeAttribute and implements IAuthorizationFilter.
namespace MyProject.Attributes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class AllowGroupsAttribute : AuthorizeAttribute, IAuthorizationFilter
{
private readonly List<PermissionGroups> groupList = null;
public AllowGroupsAttribute(params PermissionGroups[] groups)
{
groupList = groups.ToList();
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var executingUser = context.HttpContext.User;
//If the user is not authenticated then prevent execution
if (!executingUser.Identity.IsAuthenticated)
{
context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
}
}
}
}
This allows me to decorate a controller method with something like [AllowGroups(PermissionGroups.Admin, PermissionGroups.Level1]
What I plan to do, is retreive group names from appsettings.json based on the enum values listed and check that the user is a member of those groups.
My question is, what is the correct way to access the app settings from within my attribute class?
HttpContext.RequestServices
– Argumentative