![](/img/trans.png)
[英]Using RouteDataRequestCultureProvider in asp net core 3.1
[英]using input argument in attribute (Asp Core 3.1)
我有这个属性:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class AllowAccessAttribute : Attribute, IAuthorizationFilter
{
private readonly IDomainUnitOfWork unitofwork;
private readonly IHttpContextAccessor httpContext;
private readonly string _name;
public AllowAccessAttribute(string name)
{
_name = name;
}
public string Name => _name;
public AllowAccessAttribute(IDomainUnitOfWork unitofwork, IHttpContextAccessor httpContext)
{
this.unitofwork = unitofwork;
this.httpContext = httpContext;
}
public void OnAuthorization(AuthorizationFilterContext context)
{
string controller = context.RouteData.Values["controller"].ToString();
string action = context.RouteData.Values["action"].ToString();
var userId = httpContext.HttpContext.User.Identity.GetUserId<long>();
var access = string.Format("{0}:{1}", controller, action);
if (unitofwork.UserRepository.AccessLevelRepository.ValidateAccess(userId, access) == false)
context.Result = new StatusCodeResult(403);
}
}
我通过这种方式在我的行动之上调用这个属性:
[TypeFilter(typeof(AllowAccessAttribute))]
但我有问题。 我需要将参数Name
传递给这个属性。
我怎么能传递参数???
您可以像这样使用您的属性:
[AllowAccess("name")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.