阅读从appsettings.json常量在.NET核心类库

问题描述 投票:0回答:1

我需要在.NET核心(2.0)类库从appsettings.json文件中的值。有很多用于ASP.Net但对于.NET的核心解决方案。我appsettings.json像下面;

{
   {

    "serviceAccountEmail": "******@dotnet****.iam.gserviceaccount.com",
    "Certificate": "*********.p12",
    "secret": "*********",
    "ImpersonatedUser": "*********@*********.com",
    "GoogleApplicationName": "********"
    }
}
.net-core appsettings
1个回答
1
投票

你需要设置在你Startup配置

public class Startup
{
    private readonly IConfiguration _config;

    public Startup(IConfiguration config)
    {
        _config = config;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var value = _config["key"];
    }

    public void Configure(IApplicationBuilder app, IConfiguration config)
    {
        var value = config["key"];
    }
}

MS文档:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#access-configuration-during-startup

关注点分离

如果您的解决方案分成利用类库的多个项目,Microsoft.Extensions.Options.ConfigurationExtensions包就派上用场了从appsettings文件读取值,并将其注入到你的配置类项目中。

它有2个扩展名可以使用:

public static T Get<T>(this IConfiguration configuration);
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, 
    IConfiguration config) where TOptions : class;

Example:

我把使用Microsoft.AspNetCore.Identity所有与安全相关的服务整合到自己的项目叫DL.SO.Services.Security

在appsettings.json安全设置

我定义的配置,被称为“AppIdentitySettings”,身份选择我想建立在我的网站/启动项目ASP.NET Core Identity框架。

{
    "ConnectionStrings": {
        ...
    },
    "AppIdentitySettings": {
        "User": {
            "RequireUniqueEmail": true
        },
        "Password": {
            "RequiredLength": 6,
            "RequireLowercase": true,
            "RequireUppercase": true,
            "RequireDigit": true,
            "RequireNonAlphanumeric": true
        },
        "Lockout": {
            "AllowedForNewUsers": true,
            "DefaultLockoutTimeSpanInMins": 30,
            "MaxFailedAccessAttempts": 5
        }
    },
    "Recaptcha": {
        ...
    },
    ...
}

配置类

然后,你需要定义配置类,这只是波苏斯,以表示appsettings.json您的配置结构。配置类的名称不必与您在appsettings.json定义板块名称,而是属性的名称需要匹配。

namespace DL.SO.Services.Security
{
    public class AppIdentitySettings
    {
        public UserSettings User { get; set; }
        public PasswordSettings Password { get; set; }
        public LockoutSettings Lockout { get; set; }
    }

    public class UserSettings
    {
        public bool RequireUniqueEmail { get; set; }
    }

    public class PasswordSettings
    {
        public int RequiredLength { get; set; }
        public bool RequireLowercase { get; set; }
        public bool RequireUppercase { get; set; }
        public bool RequireDigit { get; set; }
        public bool RequireNonAlphanumeric { get; set; }
    }

    public class LockoutSettings
    {
        public bool AllowedForNewUsers { get; set; }
        public int DefaultLockoutTimeSpanInMins { get; set; }
        public int MaxFailedAccessAttempts { get; set; }
    }
}

以上是从和更多详细信息:https://stackoverflow.com/a/46940811/2343826

© www.soinside.com 2019 - 2024. All rights reserved.