使用来自 ADFS 的 JWT Bearer 身份验证将 ASP.NET Framework 迁移到 ASP.NET Core 3.1

Migrate ASP.NET Framework to ASP.NET Core 3.1 with JWT Bearer authentication from ADFS

ASP.NET 框架

我们有一个现有的 ASP.NET 框架网站,它使用 ActiveDirectoryFederationServicesBearerAuthentication 进行身份验证:

    app.UseActiveDirectoryFederationServicesBearerAuthentication(
            new ActiveDirectoryFederationServicesBearerAuthenticationOptions
            {
                MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
                TokenValidationParameters =
                    new TokenValidationParameters
                    {
                        ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
                        RequireSignedTokens = true
                    }
            });

“ida:AdfsMetadataEndpoint”的值为:

https://< adfs-server >/FederationMetadata/2007-06/FederationMetadata.xml

我们有一个 ADFS 3.0 服务器(运行 on Windows Server 2012 R2)发布这些 JWT 令牌。

ASP.NET 核心 3.1

由于我们将网络服务器迁移到 ASP.NET Core 3.1,我们希望继续使用相同的 ADFS 3.0 服务器向我们的网站发布 JWT 令牌。

但是我在配置新服务器以验证来自 ADFS 3.0 服务器的令牌时遇到问题。这是我尝试过的:

JwtBearerAuthentication

据我了解,JwtBearerAuthentication 需要支持 OpenIDConnect 的 ADFS 4.0?

我找到了 解决方法,它应该适用于我的情况,但在我的应用程序中存储发行者签名密钥似乎有点麻烦。如果它在 ADFS 上发生变化怎么办?

WsFederationAuthentication

我关注了这个guide,但是我觉得这个不支持Bearer认证?

结论

ActiveDirectoryFederationServicesBearerAuthentication 使用从 ADFS 3.0 颁发的 JWT 承载令牌的 ASP.NET 核心等效项是什么?

ADFS 2012 R2 可以颁发 JWT 令牌,但不支持 OIDC 发现,它不使用 openid 连接发现规范公开元数据。因此,您可以在 asp.net 核心网站 api 中使用 AddJwtBearer 扩展,但需要手动配置发行者、受众和签名密钥,作为您提供的解决方法和 here 显示的方法。当然如果你能把你的ADFS实例升级到“4.0”,这样场景会更容易实现。