Xal*_*yth 5 c# dependency-injection asp.net-web-api asp.net-core
所以我一直在尝试以 JWT 分发的形式设置一个带有令牌身份验证的小型测试 api,令牌分发部分按预期工作。
然而,由于我想让 JWT 服务的方法更加通用,以允许不同类型的令牌签名(因为我更喜欢私钥/公钥对),所以我尝试在 appsettings 文件中设置更多选项,然后决定了令牌的生成方式,我开始使用依赖注入加载这些设置,直到现在我才刚刚触及这些设置。
所以当我想要将那些我设置为单例的配置类(到目前为止我读过的大多数指南都这样做了,所以我认为它有点正确)并在ConfigureServices方法中使用它们时,问题就出现了他们添加了这些参数,以便我可以使用在我看来应该设置的参数,因为我通过获取 appsettings 文件的一部分配置了上面的几行。
然而,一旦我尝试访问它们,我就没有得到任何返回,而是留下了空值。
启动.cs
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //the token config takes values from the appsettings.json file
        var tokenConf = Configuration.GetSection("TokenConfiguration");
        services.Configure<TokenConfiguration>(tokenConf);
        //the signing credentials are assigned in the JwtTokenService constructor
        var signingConf = new SigningConfiguration();
        services.AddSingleton<SigningConfiguration>(signingConf);
        //my token service
        services.AddSingleton<IJwtTokenService, JwtTokenService>();
        //i try to get hold of the actual values to use later on
        var provider = services.BuildServiceProvider();
        TokenConfiguration tc = provider.GetService<TokenConfiguration>();
        SigningConfiguration sc = provider.GetService<SigningConfiguration>();
        //i wanna use the values in here when i set the parameters for my authentication
        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(x =>
        {
            x.Events = new JwtBearerEvents
            {
                OnTokenValidated = context =>
                {
                    return Task.CompletedTask;
                }
            };
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                //values used here since i specify issuer, audience and what kind of key to use in the settings
                //the key & credentials differ based on a bool in the settings file and will either be a symmetric or asymmetric key
                ValidIssuer = tc.Issuer,
                ValidAudience = tc.Audience,
                IssuerSigningKey = sc.Key
            };
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseMvc();
    }
}
JwtTokenService.cs(IJwtTokenService只有这里实现的CreateToken方法)
public class JwtTokenService : IJwtTokenService
{
    private TokenConfiguration tokenConf;
    public SigningConfiguration signingConf;
    public JwtTokenService(IOptions<TokenConfiguration> tc) {
        tokenConf = tc.Value;
        signingConf = new SigningConfiguration();
        //if the asymmetric bool is set to true, assign a new rsa keypair to the signing configuration
        //otherwise, use a symmetric key with a hmac hash
        if (tc.Value.AsymmetricKey)
        {
            using (var provider = new RSACryptoServiceProvider(2048))
            {
                signingConf.Key = new RsaSecurityKey(provider.ExportParameters(true));
            }
            signingConf.SigningCredentials =
                new SigningCredentials(
                    signingConf.Key,
                    SecurityAlgorithms.RsaSha256);
        }
        else {
            signingConf.Key = 
                new SymmetricSecurityKey(
                    Encoding.UTF8.GetBytes(tc.Value.HmacSecret));
            signingConf.SigningCredentials = 
                new SigningCredentials(
                    signingConf.Key, 
                    SecurityAlgorithms.HmacSha512);
        }
    }
    /// <summary>
    /// Creates a token based on the running configuration
    /// </summary>
    public string CreateToken(List<Claim> claims)
    {
        var token = new JwtSecurityToken(
            issuer: tokenConf.Issuer,
            audience: tokenConf.Audience,
            claims: claims,
            expires: DateTime.UtcNow.AddMinutes(tokenConf.Minutes),
            signingCredentials: signingConf.SigningCredentials
            );
        return new JwtSecurityTokenHandler().WriteToken(token);
    }
}
TokenConfiguration.cs
public class TokenConfiguration
{
    public string Audience { get; set; }
    public string Issuer { get; set; }
    public int Minutes { get; set; }
    public bool AsymmetricKey { get; set; }
    public string HmacSecret { get; set; }
}
签名配置.cs
public class SigningConfiguration
{
    public SecurityKey Key { get; set; }
    public SigningCredentials SigningCredentials { get; set; }
}
应用程序设置.json
"TokenConfiguration": {
"Audience": "ExampleAudience",
"Issuer": "ExampleIssuer",
"Minutes": 30,
"AsymmetricKey": true,
"HmacSecret": "example-secret-top-secret-secret-is_secret"
}
(该项目正在 asp.net core 2.1 中运行,以防万一)
我是 DI 新手,找不到很多用例与我的相同的示例,并且大多数此类案例涉及实际服务,而不是通过 DI 添加“配置”类。
可能有一种更好的方法来做到这一点,我可能只是愚蠢的没有注意到或不知道谷歌到底要做什么才能得到正确的答案,无论如何,在这之后很可能会观看/阅读一些关于 DI 的内容。
任何意见或想法都将受到高度赞赏,因为我对 ASP.NET Core 和整个流程仍然很陌生。
作为关于私钥生成的一个小问题,就我而言,最好将生成的密钥对存储在密钥库中,或者将其存储在内存中,或者通过 openSSL 之类的东西生成它们并在启动时读取它们最好的选择?
您正在请求TokenConfiguration而不是IOptions<TokenConfiguration>向服务提供商请求。更改此行
   TokenConfiguration tc = provider.GetService<TokenConfiguration>();
   SigningConfiguration sc = provider.GetService<SigningConfiguration>(); 
和
    IOptions<TokenConfiguration> tc = provider.GetService<IOptions<TokenConfiguration>>();
    IOptions<SigningConfiguration> sc = provider.GetService<IOptions<SigningConfiguration>>();
然后使用 访问选项tc.Value。
构建并不是一个好主意,我会直接从配置中获取ServiceProvider我需要的任何地方。ConfigureServicesConfiguration["TokenConfiguration:Audience"]ConfigureServices
| 归档时间: | 
 | 
| 查看次数: | 6031 次 | 
| 最近记录: |