我试图了解如何将存储在现有数据库(位于:localhost:3306)中的用户(电子邮件、密码、名字、姓氏和操作系统)绑定(bind)到我的 identityserver4 项目中,以便我可以使用这些信息来登录用户或注册新用户到该数据库?
我阅读了一些教程(特别是 http://docs.identityserver.io/en/release/quickstarts/8_entity_framework.html),但我认为这始终适用于同一个项目中的数据库。我的数据库不在同一个项目中。
在这种情况下,我阅读了有关 asp.net-core Identity 的信息。但我不完全理解那是如何相关的。
谁能告诉我如何在我的项目中绑定(bind)数据库以及身份与应用程序用户等的作用是什么?
提前致谢
最佳答案
这篇文章更符合你的情况。您链接的是配置数据而不是用户数据: http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html
简而言之,您想通过 Asp.Net Core Identity 访问您的用户数据。 你需要:
- 制作一个包含相关字段的用户类作为你的数据库
- 创建一个 EntityFramework DbContext 类以将您的数据库映射到您的类
- 使用 aspnet core 身份注册您的用户类和 dbcontext
- 告诉 IdentityServer 使用 AspNetIdentity
这就是您的 Startup ConfigureServices 方法在实现后的样子。此处未显示您需要创建的 DbContext 和 User 类。
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<YourUserStoreDbContextHere>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<YourUserClassHere, YourRoleClassHereIfAny>()
.AddEntityFrameworkStores<YourUserStoreDbContextHere>()
.AddDefaultTokenProviders();
services.AddIdentityServer()
// Other config here
.AddAspNetIdentity<YourUserClassHere>();
}
有关配置用户类和 dbcontext 的详细信息,请参阅 AspNet Identity 上的文档:https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
关于c# - 如何从 identityServer4 的现有数据库中获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983056/