Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET内核中使用IAsyncAuthorizationFilter进行依赖项注入_C#_Asp.net Core_.net Core_Autofac - Fatal编程技术网

C# 如何在ASP.NET内核中使用IAsyncAuthorizationFilter进行依赖项注入

C# 如何在ASP.NET内核中使用IAsyncAuthorizationFilter进行依赖项注入,c#,asp.net-core,.net-core,autofac,C#,Asp.net Core,.net Core,Autofac,我已经实现了类的IAsyncAuthorizationFilter接口;此外,该类是从属性派生的,因此我可以使用它标记控制器类和操作方法。到目前为止,这是可行的;authorizationAsync(AuthorizationFilterContext context)方法在action方法之前被调用,如果请求未经过身份验证,则为响应返回一个HTTP 401 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 公共

我已经实现了类的
IAsyncAuthorizationFilter
接口;此外,该类是从
属性
派生的,因此我可以使用它标记控制器类和操作方法。到目前为止,这是可行的;authorizationAsync(AuthorizationFilterContext context)方法在action方法之前被调用,如果请求未经过身份验证,则为响应返回一个
HTTP 401

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类CustomAuthenticationAttribute:属性,IAsyncAuthorizationFilter
{
AuthorizationAsync(AuthorizationFilterContext上下文)上的公共异步任务
{
...
}
}
此属性的用法如下所示

[TypeFilter(typeof(CustomAuthenticationAttribute))]
public class SomeDataController : Controller
{
    [HttpGet]
    public async Task GetData()
    {
        ...
    }
} 
[CustomAuthenticationAttribute]
公共类SomeDataController:控制器
{
[HttpGet]
公共异步任务GetData()
{
...
}
}
现在,我想使用一个应用程序服务(它从数据库获取身份验证所需的秘密私钥信息),并尝试使用属性注入。通过ctor注入依赖项在这里不是一个好的选择,因为它是作为一个属性实现的。所以我试着

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
公共密封类CustomAuthenticationAttribute:属性,IAsyncAuthorizationFilter
{
公共IPrivateKeyLookupService密钥服务{get;set;}
AuthorizationAsync(AuthorizationFilterContext上下文)上的公共异步任务
{
字符串公钥=。。。
...
var privateKey=wait this.KeyService.GetPrivateKeyFrom(publicKey);
...
}
}
……但财产注入在这里似乎不起作用。该服务已向IoC注册,但属性未连接。这是一个ASP.NET Core 1.1项目,我在其中使用了
Autofac
。在
Startup
类中,我的
ConfigureServices
方法类似于

public void配置服务(IServiceCollection集合)
{
...
var containerBuilder=新的containerBuilder();
containerBuilder.RegisterType().As();
containerBuilder.Populate(服务);
this.container=containerBuilder.Build();
}

Autofac
是否支持
IAsyncAuthorizationFilter
类型的自动布线?如果没有,我该如何填充该功能

结果很简单

ASP.NET Core
中有
TypeFilter
属性,该属性也是一个过滤器,用于创建另一个过滤器(由
Type
指定),并通过涉及依赖项注入来满足其构造函数参数

我更改了IAsyncAuthorizationFilter的实现;删除了属性并添加了一个ctor,因为使用
TypeFilter
ctor注入现在可以工作了

公共密封类CustomAuthenticationAttribute:IAsyncAuthorizationFilter
{
私有只读IPrivateKeyLookupService密钥服务;
公共CustomAuthenticationAttribute(
IPrivateKeyLookupService键服务)
{
this.keyService=keyService;
}
AuthorizationAsync(AuthorizationFilterContext上下文)上的公共异步任务
{
...
}
}
我还从
Attribute
中删除了继承,因为不再需要它,也不再需要
AttributeUsage
属性的声明

所以,我可以使用我的属性如下

[TypeFilter(typeof(CustomAuthenticationAttribute))]
public class SomeDataController : Controller
{
    [HttpGet]
    public async Task GetData()
    {
        ...
    }
} 

还可以通过
TypeFilter
类的
object[]arguments
属性将其他参数传递给过滤器的ctor。

您是否尝试过在控制器上使用
[TypeFilter(typeof(CustomAuthenticationAttribute))]
注册过滤器?你可以在这里获得更多信息:@juunas谢谢;几分钟前才找到解决方案。这是典型的。。。我找了几个小时,然后在我问了这个问题之后,解决方案出现了(-;巨大的道具。这绝对是检查api密钥的简单方法