如何从ASP.NET Core中的ActionFilterAttribute访问AppSettings [英] How can I access AppSettings from an ActionFilterAttribute in ASP.NET Core

查看:310
本文介绍了如何从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

  • <$ c在您的属性上实现的$ c> IFilterFactory

    • ServiceFilterAttribute
    • TypeFilterAttribute
    • IFilterFactory implemented on your attribute
    • 这篇关于如何从ASP.NET Core中的ActionFilterAttribute访问AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆