【问题标题】:Create custom .well-known\openid-configuration in .net core在 .net 核心中创建自定义 .well-known\openid-configuration
【发布时间】:2022-01-14 12:11:13
【问题描述】:

我是 JWT 身份验证的新手,所以我想做的可能是错误的。

我正在使用非对称 RSA 密钥对来签署和验证 JWT。

在我的 startup.cs 中我有:

 public void ConfigureServices(IServiceCollection services) {
            services.AddControllers();
          
            services.AddSingleton<RsaSecurityKey>(provider => {
                RSA rsa = RSA.Create();
                rsa.ImportRSAPublicKey(
                    source: Convert.FromBase64String(configuration["Jwt:PublicKey"]),
                    bytesRead: out int _
                );
                
                return new RsaSecurityKey(rsa);
            });
            services.AddAuthentication()
                .AddJwtBearer("Asymmetric", options => {
                    SecurityKey rsa = services.BuildServiceProvider().GetRequiredService<RsaSecurityKey>();
                    
                    options.TokenValidationParameters = new TokenValidationParameters {
                        IssuerSigningKey = rsa,
                        ValidAudience = "audience-test",
                        ValidIssuer = "test-issuer",
                        RequireSignedTokens = true,
                        RequireExpirationTime = true,
                        ValidateLifetime = true,
                        ValidateAudience = true,
                        ValidateIssuer = true,
                    };
                });
        }

要生成我在控制器中的令牌:

[HttpPost]
        [Route("Asymmetric")]
        public IActionResult GenerateTokenAsymmetric()
        {
            using RSA rsa = RSA.Create();
            rsa.ImportRSAPrivateKey(
                source: Convert.FromBase64String(_configuration["Jwt:PrivateKey"]),
                bytesRead: out int _);

            var signingCredentials = new SigningCredentials(
                key: new RsaSecurityKey(rsa),
                algorithm: SecurityAlgorithms.RsaSha256
            );

            DateTime jwtDate = DateTime.Now;

            var jwt = new JwtSecurityToken(
                audience: "test-audience",
                issuer: "test-issuer",
                claims: new Claim[] { new Claim(ClaimTypes.NameIdentifier, "John") },
                notBefore: jwtDate,
                expires: jwtDate.AddMinutes(1),
                signingCredentials: signingCredentials
            );

            string token = new JwtSecurityTokenHandler().WriteToken(jwt);

            return Ok(new
            {
                jwt = token
            });
        }

如您所见,我的公钥存储在我的 appsettings 中。 我想使用 options.MetadataAddress 以便我的客户可以下载有关我的 api 的元数据并检索公钥以验证令牌。

我的问题是:

可以在 .net core 中创建自定义 .well-known/openid-configuration 吗?或者我必须使用 IdentityServer 例如?

感谢您的帮助

【问题讨论】:

    标签: asp.net-core jwt rsa openid-connect


    【解决方案1】:

    我认为创建自己的静态“假”页面并不难。well-known/openid-configuration 页面。但与此同时,拥有像 IdentityServer 这样的专用令牌服务将在您的系统增长时为您带来优势。不要忘记您还需要创建 JWKS 端点。由于 AddJwBearer 处理程序向两者发出请求。

    此外,您自己完成这一切也会带来现有解决方案中已经修复/解决的潜在安全问题。

    【讨论】:

    • 感谢您的反馈,所以我正在使用 IdentityServer4,在方法 services.AddSigningCredential(MyCredential) 中,MyCredential 是由生成 RSA 密钥的方法生成的,这样做是一个好习惯吗?我帮助自己写这篇文章link
    • 如果你使用 IdentityServer,你不应该尝试自己制作。在生产中使用 AddSigningCredential 是一个好主意,您只需向它传递一个私钥,这就是您应该这样做的方式。 AddSigningCredential 不会为您创建任何密钥,您必须在 IdentityServer 之外生成并创建它。也许使用 Openssl 或 Azure Key Vault 或类似的东西......
    • 您希望我在回答中澄清什么吗?如果您认为它回答了您的问题,请随时接受它
    • 不,这对我来说很好:) 我已经接受了你的回答,我继续我的 api,我正在寻找如何刷新令牌,如果我需要一些帮助,我会回来的 :)
    • 欢迎提出更多问题,我会尽力解答!
    猜你喜欢
    • 2020-04-02
    • 2019-02-08
    • 2022-01-08
    • 2016-10-08
    • 2021-09-09
    • 2017-06-23
    • 1970-01-01
    • 2021-11-17
    • 2018-10-07
    相关资源
    最近更新 更多