Configuration 使用设置配置asp.net core

Configuration 使用设置配置asp.net core,configuration,web-config,asp.net-core,.net-core,Configuration,Web Config,Asp.net Core,.net Core,我正在评估asp.net core和.net core,但有些事情我还不确定。在过去,可以使用web.config开箱即用配置许多组件。 举几个例子: 有成员资格提供程序,我可以实现许多提供程序,但我可以稍后配置应该使用哪个提供程序。这取决于用例。现在我应该使用asp.net标识,但我只能找到在源代码中执行的配置 身份验证也一样。我可以定义“CookieAuthentication”,并且必须在源代码中设置名称、登录路径或超时。在过去,我可以设置超时,等等。。。通过web.config 是否

我正在评估asp.net core和.net core,但有些事情我还不确定。在过去,可以使用web.config开箱即用配置许多组件。 举几个例子:

  • 有成员资格提供程序,我可以实现许多提供程序,但我可以稍后配置应该使用哪个提供程序。这取决于用例。现在我应该使用asp.net标识,但我只能找到在源代码中执行的配置
  • 身份验证也一样。我可以定义“CookieAuthentication”,并且必须在源代码中设置名称、登录路径或超时。在过去,我可以设置超时,等等。。。通过web.config

是否有任何方法可以从配置文件中对这些内容进行开箱即用的部分配置?或者这不再受支持,我必须自己实现这个配置?在过去,这是一种非常舒适的方式。

在ASP.NET Core中,Web.config文件仅用于IIS配置,您不能将其用于应用程序配置,但您可以使用新的、更好的、更灵活的配置选项

您可以使用多个配置源,但在本例中,我使用的是json。这些示例来自我的项目中的工作代码

您可以从配置文件在启动时配置内容

首先,添加一个json格式的配置文件,该文件映射到类。您可以看到,以及它所映射的json文件

然后,在ConfigureServices方法中,将类配置为从配置系统连接,如图所示

services.Configure<SimpleAuthSettings>(Configuration.GetSection("SimpleAuthSettings"));
services.Configure(Configuration.GetSection(“SimpleAuthSettings”);
然后将类的IOOptions访问器添加到Startup.cs中Configure方法的方法签名中 依赖项注入将为您将其注入该方法,以便您可以在那里使用它来配置内容。具体地说,我正在从设置对象设置cookie身份验证方案和名称

值得注意的是,您可以向Configure方法签名添加任何您想要的内容,只要它是已在ConfigureServices方法中注册的内容,DI就可以为您注入它

public class Startup
{
    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        // this file is the custom configuration file to hydrate my settings from
        builder.AddJsonFile("simpleauthsettings.json", optional: true);
        ....
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        ....
        services.Configure<SimpleAuthSettings>(Configuration.GetSection("SimpleAuthSettings"));
        ....

    }

    // note that the DI can inject whatever you need into this method signature
    // I added IOptions<SimpleAuthSettings> authSettingsAccessor to the method signature
    // you can add anything you want as long as you register it in ConfigureServices
    public void Configure(
        IApplicationBuilder app, 
        IHostingEnvironment env, 
        ILoggerFactory loggerFactory,
        IOptions<SimpleAuthSettings> authSettingsAccessor  
        )
    {
        ...
        // Add cookie-based authentication to the request pipeline
        SimpleAuthSettings authSettings = authSettingsAccessor.Value;

        var ApplicationCookie = new CookieAuthenticationOptions
        {
            AuthenticationScheme = authSettings.AuthenticationScheme,
            CookieName = authSettings.AuthenticationScheme,
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            LoginPath = new PathString("/Login/Index"),
            Events = new CookieAuthenticationEvents
            {
                //OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
            }
        };

        app.UseCookieAuthentication(ApplicationCookie);

        // authentication MUST be added before MVC
        app.UseMvc();
    }   
}
公共类启动
{
公开启动(IHostingEnvironment env、IApplicationEnvironment appEnv)
{
var builder=new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true);
//此文件是自定义配置文件,用于从中删除我的设置
AddJsonFile(“simpleauthsettings.json”,可选:true);
....
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;set;}
public void配置服务(IServiceCollection服务)
{
....
services.Configure(Configuration.GetSection(“SimpleAuthSettings”);
....
}
//请注意,DI可以将您需要的任何内容注入此方法签名
//我将IOptions authSettingsAccessor添加到方法签名中
//只要在ConfigureServices中注册,就可以添加所需的任何内容
公共无效配置(
IApplicationBuilder应用程序,
IHostingEnvironment环境,
伊洛格工厂伐木厂,
IOptions authSettingsAccessor
)
{
...
//将基于cookie的身份验证添加到请求管道
SimpleAuthSettings authSettings=authSettingsAccessor.Value;
var applicationcokie=新CookieAuthenticationOptions
{
AuthenticationScheme=authSettings.AuthenticationScheme,
CookieName=authSettings.AuthenticationScheme,
自动验证=真,
自动挑战=正确,
LoginPath=新路径字符串(“/Login/Index”),
事件=新CookieAuthenticationEvents
{
//OnValidatePrincipal=SecurityStampValidator.ValidatePrincipalAsync
}
};
app.useCookie认证(ApplicationCookie);
//必须在MVC之前添加身份验证
app.UseMvc();
}   
}

谢谢-但这意味着我必须自己创建配置结构?(过去开箱即用的内容)是的,这是依赖注入的新世界,不再有静态配置,我的建议是接受它。需要理解的是,为了使ASP.NET核心跨平台,他们必须将其与IIS分离。依赖注入确实是一种更好的配置方式,一旦你习惯了它,你就会爱上它。因此,您可以坚持使用旧平台的舒适性及其熟悉的方式,也可以跳入新平台,熟悉并熟悉新平台。要使用新的ASP.NET核心,您将不得不放弃许多旧想法并接受新想法。这很好。我只是想确定我没有忽略什么。。。因此,我强烈要求;)
public class Startup
{
    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        // this file is the custom configuration file to hydrate my settings from
        builder.AddJsonFile("simpleauthsettings.json", optional: true);
        ....
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        ....
        services.Configure<SimpleAuthSettings>(Configuration.GetSection("SimpleAuthSettings"));
        ....

    }

    // note that the DI can inject whatever you need into this method signature
    // I added IOptions<SimpleAuthSettings> authSettingsAccessor to the method signature
    // you can add anything you want as long as you register it in ConfigureServices
    public void Configure(
        IApplicationBuilder app, 
        IHostingEnvironment env, 
        ILoggerFactory loggerFactory,
        IOptions<SimpleAuthSettings> authSettingsAccessor  
        )
    {
        ...
        // Add cookie-based authentication to the request pipeline
        SimpleAuthSettings authSettings = authSettingsAccessor.Value;

        var ApplicationCookie = new CookieAuthenticationOptions
        {
            AuthenticationScheme = authSettings.AuthenticationScheme,
            CookieName = authSettings.AuthenticationScheme,
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            LoginPath = new PathString("/Login/Index"),
            Events = new CookieAuthenticationEvents
            {
                //OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
            }
        };

        app.UseCookieAuthentication(ApplicationCookie);

        // authentication MUST be added before MVC
        app.UseMvc();
    }   
}