public class PermissionAttribute : Attribute, IAuthorizationFilter
{
private readonly IPermissionService _Servie;
private readonly string _moduleIdentityName;
private readonly string _permissionCode;
public PermissionAttribute(string moduleIdentityName, string permissionCode)
{
_moduleIdentityName = moduleIdentityName;
_permissionCode = permissionCode;
}
public void OnAuthorization(AuthorizationFilterContext filterContext)
{
}
}
//怎么注入 IPermissionService _Servie
[Permission("cms", "view")]
public IActionResult Index()
{
return View();
}
//Startup.cs
services.AddTransient<IPermissionService, PermissionService>();
PermissionAttribute 实现 IFilterFactory 接口,参考 How can I use Dependency Injection in a .Net Core ActionFilterAttribute?
[TypeFilter(typeof(PermissionAttribute),Arguments =new object[] { "cms","view" })]
public IActionResult Index()
和
filterContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
不知道2个哪个好一些
直接构造函数中加入IPermissionService 参数就行了啊
还有其他参数
filterContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
谢谢 方法可行