ASP.NET Core 2.1 Web API 添加JWT 认证的时候是否需要像AspNetUsers 这样创建表?

Is it necessary to create tables like AspNetUsers while adding JWT authentication to ASP.NET Core 2.1 Web API?

我正在尝试创建一个简单的 ASP.NET Core 2.1 Web API 来提供来自 SQL Server 数据库的数据。我想使用基于 JWT 的承载身份验证通过简单的 usernamepassword 凭据访问 API,没有什么太复杂的。到目前为止,我已经能够创建一个简单的 Core 2.1 Web API 项目,并启用了未经身份验证的 CRUD 操作,但向其添加 JWT 支持被证明有点问题。

我的数据库有一个名为 Users 的 table,它包含用户的个人信息,其中包括 usernamepassword hash 字段。我想使用相同的 table 进行基于 JWT 的身份验证。然而,大多数关于该主题的教程(如 this)都使用迁移的概念,它向我的数据库添加了几个 table(例如 AspNetUserTokensAspNetUserLogins),这对我来说似乎毫无用处,因为我不打算使用 Google 或 Facebook 等外部身份验证。即使 table AspNetUsers 也包含看似多余的字段。

我也尝试使用内存数据存储跟随 this tutorial 但无法实现 SQL 服务器集成(面临很多错误,例如 Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager'Cannot create a DbSet for 'IdentityUser' this type is not included in the model for the context).

我可以将基于 JWT 的身份验证添加到 ASP.NET Core 2.1 Web API 与 SQL 服务器数据库集成而无需迁移过程(特别是在 this project)?

Can I add JWT-based authentication to an ASP.NET Core 2.1 Web API with SQL Server database integration without the migration procedure (especially in the context of this project)?

是的,可以。 您不需要仅仅因为处理用户数据就使用 IdentityServer( 及其迁移过程)。 您可以完美地创建自己的用户处理实现,如 link

中的那样

要添加 SQL 服务器集成,查看链接项目中的 StartUp.cs class,这一行

services.AddDbContext<DataContext>(x => x.UseInMemoryDatabase("TestDb"));

更改该行并指出您的数据库连接字符串

services.AddDbContext<DataContext>(x => x.UseSqlServer("yourConnectionstring"));