如何从ASP.NET Core中的ActionFilterAttribute访问AppSettings

编程入门 行业动态 更新时间:2024-10-24 16:23:01
本文介绍了如何从ASP.NET Core中的ActionFilterAttribute访问AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在尝试开发一个WebAPI(.NET Core),该WebAPI具有一些应使用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

本文发布于:2023-11-02 15:01:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   Core   AppSettings   ActionFilterAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!