【发布时间】:2019-03-28 20:33:21
【问题描述】:
在我的 ASP.NET Core MVC 应用程序中,我有一个继承自 AuthorizeAttribute 并实现 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);
}
}
}
}
这让我可以用 [AllowGroups(PermissionGroups.Admin, PermissionGroups.Level1] 之类的东西来装饰控制器方法
我打算做的是根据列出的枚举值从 appsettings.json 中检索组名,并检查用户是否是这些组的成员。
我的问题是,从我的属性类中访问应用程序设置的正确方法是什么?
【问题讨论】:
-
在启动时通过选项或具体对象模型配置设置,然后通过
HttpContext.RequestServices解决它们
标签: c# asp.net-core-mvc appsettings authorize-attribute iauthorizationfilter