繁体   English   中英

在属性中使用输入参数(Asp 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM