如何从 ASP.NET Core 中的 ActionFilterAttribute 访问 AppSettings [英] How can I access AppSettings from an ActionFilterAttribute in ASP.NET Core
问题描述
我目前正在尝试开发一个 WebAPI (.NET Core),它有一些应该使用 HTTP 基本身份验证的控制器操作.为了实现这一点,我编写了一个 ActionFilterAttribute,然后我可以在我的控制器中使用它来限制对某些操作的访问.如果我做这样的事情,这一切都很好:
I am currently trying to develop a WebAPI (.NET Core) which has some controller actions that should use HTTP Basic Authentication. To implement this, I have written an ActionFilterAttribute which I can then use in my controller to restrict access to certain actions. This all workes well if I do something like this:
BasicAuthAttribute.cs
public class BasicAuthAttribute : ActionFilterAttribute{
private string _username { get; set; }
private string _password { get; set; }
public BasicAuthAttribute(string username, string password) {
_username = username;
_password = password;
}
public override void OnActionExecuting(ActionExecutingContext actionContext) {
//do Auth check...
}
}
在控制器中,我按如下方式使用它:
In the controller I then use it as following:
SomeController.cs
[BasicAuth("testuser","testpassword")]
[HttpGet("{id}")]
public IActionResult Get(string id) {
return new ObjectResult("Test");
}
现在我不想在 SomeController.cs 中指定用户名和密码.相反,我想将它们存储在 appsettings.json 中.如何在 ActionFilterAttribute 的 OnActionExecuting 方法中访问存储在 appsettings.json 中的值?
Now I do not want to specify username ond password in SomeController.cs. Instead I would like to store them in appsettings.json. How is it possible to access values stored in appsettings.json in the OnActionExecuting method in the ActionFilterAttribute?
如果我将 BasicAuthAttribute 的构造函数更改为以下内容,.Net 希望我通过设置,这是不可能的.依赖注入在这里似乎不起作用.
If I change the constructor of BasicAuthAttribute to the following, .Net expects me to pass the settings, which is not possible. Dependency Injection seems not to work here.
public BasicAuthAttribute(IOptions<AppSettings> appSettings) {}
任何帮助或想法将不胜感激
Any help or ideas would be appreciated
更新基于 Set 的回答:
我最终将属性更改为过滤器.如果其他人需要它,请参阅下面的工作解决方案:
I ended up changing the attribute to a filter. In case anybody else needs it see the working solution below:
BasicAuthFilter.cs
public class BasicAuthFilter : IActionFilter {
protected AppSettings _settings { get; set; }
public BasicAuthAttribute(IOptions<AppSettings> appSettings) {
_settings = appSettings;
}
public void OnActionExecuted(ActionExecutedContext context) {
//nothing to do here
}
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
//do Auth check...
}
}
SomeController.cs
public class SomeController : Controller {
[TypeFilter(typeof(BasicAuthFilter))]
[HttpGet("{id}")]
public IActionResult Get(string id) {
return new ObjectResult("Test");
}
}
推荐答案
ASP.NET Core 文档 解释了如何使用 DI:
Filters section in ASP.NET Core documentation explains how to use DI:
如果您的过滤器具有需要从 DI 访问的依赖项,则有几种支持的方法.您可以使用以下方法之一将过滤器应用于类或操作方法:
If your filters have dependencies that you need to access from DI, there are several supported approaches. You can apply your filter to a class or action method using one of the following:
ServiceFilterAttribute
TypeFilterAttribute
IFilterFactory
在您的属性上实现ServiceFilterAttribute
TypeFilterAttribute
IFilterFactory
implemented on your attribute
这篇关于如何从 ASP.NET Core 中的 ActionFilterAttribute 访问 AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!