c# - 如何从 identityServer4 的现有数据库中获取用户

标签 c# mysql asp.net-core asp.net-identity identityserver4

我试图了解如何将存储在现有数据库(位于: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/

相关文章:

c# - 在 Dynamics 365 API 上运行查询

单线程应用程序中的 C# 事件处理

c# - 在没有单元测试框架的情况下使用 Specflow

ASP.NET Core EF6 身份

angular - ASP.NET核心: Windows Authentication with OPTIONS exception (CORS preflight)

c# - 如何创建使用表格作为数据源的折线图?

mysql - PHP PDO 在 Mysql 中插入 Longditude/latituded 并在插入前检查是否存在

mysql - 向表中添加 SQL 列会复制该表

mysql - php mysql 股市交易游戏 - 计算盈亏

asp.net-core - 在 dotnet 构建的当前目录中找不到 'project.json'