使用 IdentityServer4 的 Blazor WebAssembly 身份验证,Asp.Net 没有 Entity Framework 的核心身份和自定义提供程序

Blazor WebAssembly Authentication with IdentityServer4, Asp.Net Core Identity and custom provider without Entity Framework

我有一个 Blazor 服务器端应用程序,它使用不使用 Entity Framework 的 CosmosDB 自定义身份提供程序。我想将应用程序转换为托管在 Asp.net Core.

上的 Blazor WebAssembly

我的 Blazor 服务器端 Startup.cs 自定义提供程序如下所示:

 services.AddTransient<IRoleStore<ApplicationRole>, CosmosDBRoleStore<ApplicationRole>>();
 services.AddTransient<IUserStore<ApplicationUser>, CosmosDBUserStore<ApplicationUser>>();
 services.AddIdentity<ApplicationUser, ApplicationRole>().AddDefaultTokenProviders();
 services.AddAuthentication();
 services.AddAuthorization();

Visual Studio 中用于 Blazor WebAssembly 托管在 Asp.net Core 上的当前模板在 Asp.Net Core Identity + SQL 服务器,它需要来自 Entity Framework 核心的 DBContext。

 services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

  services.AddDefaultIdentity<ApplicationUser>(options => 
                options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();

  services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
  services.AddAuthentication().AddIdentityServerJwt();

我安装了自定义 Cosmos DB 身份提供程序,但是行

 services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

需要 Entity Framework DBContext 来填充 IdentityServer4 的所有参数。

我分析了四种情况:

1) 在我的自定义身份提供者之上使用 Entity Framework,但它几乎需要完全重新编码,因为它使用了深度自定义的 CosmosDB 与自定义 Login/Logout/Signup 等的集成。 pages

2) 在我的自定义提供者之上使用 Identity Server4,但我是新手,整个框架很复杂,所有带有 Asp.Net Core Identity 的示例也使用 Entity Framework我不知道该怎么做。

3) 删除我的自定义身份提供程序并使用 Azure B2C。经过一些测试后,我发现它混乱且难以定制,带有弹出窗口和未标记的确认电子邮件。这也将使数周的自定义配置文件编辑页面工作无效。

4) 将项目保留在 Blazor 服务器端,不要迁移

有什么我遗漏的可以帮助解决问题的吗?任何可以帮助使用不使用 Entity Framework?

的自定义身份提供者配置 IdentityServer4 的库

经过几天的测试,终于实现了我想要的:

“将 Blazor Server 应用程序移植到 Asp.net Core 上托管的 Blazor WebAssembly,保留我的自定义 Asp.net Identity + CosmosDB 提供程序,而不使用 Identity Server 4 和 Entity Framework."

我创建了 JWT 令牌身份验证服务 + 状态提供程序的手动实现,并将其挂接到 Asp.Net 核心身份。

我使用这个项目作为指南,其中包含一个完整的工作 JWT 实现:

https://github.com/CodeMazeBlog/blazor-series/tree/blazor-webassembly-authentication-aspnetcore-identity