简体   繁体   English

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

[英]How can I access AppSettings from an ActionFilterAttribute in ASP.NET Core

I am currently trying to develop a WebAPI (.NET Core) which has some controller actions that should use HTTP Basic Authentication.我目前正在尝试开发一个 WebAPI (.NET Core),它有一些应该使用 HTTP 基本身份验证的控制器操作。 To implement this, I have written an ActionFilterAttribute which I can then use in my controller to restrict access to certain actions.为了实现这一点,我编写了一个 ActionFilterAttribute,然后我可以在我的控制器中使用它来限制对某些操作的访问。 This all workes well if I do something like this:如果我做这样的事情,这一切都很好:

BasicAuthAttribute.cs 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一些控制器.cs

[BasicAuth("testuser","testpassword")]
[HttpGet("{id}")]
public IActionResult Get(string id) {
    return new ObjectResult("Test");
}

Now I do not want to specify username ond password in SomeController.cs.现在我不想在 SomeController.cs 中指定用户名和密码。 Instead I would like to store them in appsettings.json.相反,我想将它们存储在 appsettings.json 中。 How is it possible to access values stored in appsettings.json in the OnActionExecuting method in the ActionFilterAttribute?如何在 ActionFilterAttribute 的 OnActionExecuting 方法中访问存储在 appsettings.json 中的值?

If I change the constructor of BasicAuthAttribute to the following, .Net expects me to pass the settings, which is not possible.如果我将 BasicAuthAttribute 的构造函数更改为以下内容,.Net 希望我通过设置,这是不可能的。 Dependency Injection seems not to work here.依赖注入在这里似乎不起作用。

public BasicAuthAttribute(IOptions<AppSettings> appSettings) {}

Any help or ideas would be appreciated任何帮助或想法将不胜感激

UPDATE based on Set's answer:根据 Set 的回答更新:

I ended up changing the attribute to a filter.我最终将属性更改为过滤器。 In case anybody else needs it see the working solution below:如果其他人需要它,请参阅下面的工作解决方案:

BasicAuthFilter.cs 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一些控制器.cs

public class SomeController : Controller {
   [TypeFilter(typeof(BasicAuthFilter))]
   [HttpGet("{id}")]
   public IActionResult Get(string id) {
        return new ObjectResult("Test");
   }
}   

Filters section in ASP.NET Core documentation explains how to use DI: ASP.NET Core 文档中的过滤器部分解释了如何使用 DI:

If your filters have dependencies that you need to access from DI, there are several supported approaches.如果您的过滤器具有需要从 DI 访问的依赖项,则有几种受支持的方法。 You can apply your filter to a class or action method using one of the following:您可以使用以下方法之一将过滤器应用于类或操作方法:

  • ServiceFilterAttribute
  • TypeFilterAttribute
  • IFilterFactory implemented on your attribute IFilterFactory在您的属性上实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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