相关疑难解决方法(0)

如何在ASP.NET Core中创建自定义AuthorizeAttribute?

我正在尝试在ASP.NET Core中创建自定义授权属性.在以前的版本中,可以覆盖bool AuthorizeCore(HttpContextBase httpContext).但这不再存在于AuthorizeAttribute.

制作自定义AuthorizeAttribute的当前方法是什么?

我想要完成的任务:我在Header Authorization中收到一个会话ID.从该ID我将知道特定动作是否有效.

c# asp.net authorization asp.net-core-mvc asp.net-core

362
推荐指数
13
解决办法
19万
查看次数

如何在asp.net中的动作过滤器中添加参数?

我有以下过滤器属性,我可以像这样将字符串数组传递给属性[MyAttribute("string1", "string2")].

public class MyAttribute : TypeFilterAttribute
{
    private readonly string[] _ids;

    public MyAttribute(params string[] ids) : base(typeof(MyAttributeImpl))
    {
        _ids = ids;
    }

    private class MyAttributeImpl : IActionFilter
    {
        private readonly ILogger _logger;

        public MyAttributeImpl(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<MyAttribute>();
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            // HOW DO I ACCESS THE IDs VARIABLE HERE ???
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何将字符串数组传递_ids给动作过滤器的实现?我错过了一些非常明显的东西!?

c# custom-action-filter actionfilterattribute asp.net-core-mvc asp.net-core

16
推荐指数
1
解决办法
9075
查看次数

如何在ASP.NET CORE中使用具有依赖注入的动作过滤器?

我在我的ASP.NET CORE应用程序中使用基于构造函数的依赖注入,我还需要在我的动作过滤器中解决依赖关系:

public class MyAttribute : ActionFilterAttribute
{
    public int Limit { get; set; } // some custom parameters passed from Action
    private ICustomService CustomService { get; } // this must be resolved

    public MyAttribute()
    {
    }

    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        // my code
        ...

        await next();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

[MyAttribute(Limit = 10)]
public IActionResult()
{
    ...
Run Code Online (Sandbox Code Playgroud)

如果我把ICustomService放到构造函数中,那么我就无法编译我的项目了.那么,我如何在动作过滤器中获取接口实例呢?

c# asp.net asp.net-mvc action-filter asp.net-core

14
推荐指数
3
解决办法
1万
查看次数

访问asp.net核心rc2应用程序中ActionFilterAttribute中的IConfiguration

我正在编写将验证验证码的属性.为了正常工作,它需要知道秘密,我保留在设置中(秘密管理器工具).但是我不知道如何从属性类中读取配置.asp.net核心中的DI支持构造函数注入(并且不支持属性注入),因此这将产生编译错误:

public ValidateReCaptchaAttribute(IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            this.m_configuration = configuration;
        }
Run Code Online (Sandbox Code Playgroud)

因为我装饰方法时[ValidateReCaptcha]我无法传递配置

那么如何从属性类中的方法中读取config中的内容呢?

asp.net asp.net-core

6
推荐指数
2
解决办法
2885
查看次数