【问题标题】:How to customise table name from identityserver4?如何从 identityserver4 自定义表名?
【发布时间】:2020-08-23 00:18:08
【问题描述】:

我正在学习 aspnet.core 并关注与 IdentityServer4 相关的 tutorial,用于客户端应用程序的 Oauth2 服务器,在本例中是它的反应应用程序。

按照教程学习后,我意识到我所有的表都有前缀AspNet

不过,在 IdentityServer4 docs 中,我可以看到他们为不同的客户端(API、客户端)创建了具有不同前缀的表。我一直在尝试阅读文档,搜索我从中获得的脚手架模板:

dotnet new react -o <output_directory_name> -au Individual

但我在任何地方都找不到。我什至在命名空间using Microsoft.AspNetCore.Identity; 的上下文中查看了IdentityUser 类,它们的表名没有前缀AspNet。

我来自 laravel,因此基本上我在 c# 和 aspnet 核心方面的经验几乎为零。谁能帮我解释一下?

谢谢

【问题讨论】:

  • 不要粗鲁,但实际上,从 Hello World 代码集开始,充分理解它,然后转向更复杂的示例。
  • 我做到了。我关注了 API 教程,然后我关注了 ef core 教程一段时间,直到我需要身份验证,所以我关注了 SPA 中的身份验证教程。这个问题实际上与教程有关......
  • 我也读过这个问题stackoverflow.com/questions/51483885/…,但我找不到在“Startup.cs”上编辑的表格。我只想知道模板是如何做到的。我错过了什么吗?
  • 我认为答案(关键部分)是services.AddIdentityServer() .AddConfigurationStore(options => {.Invoke(o, new object[] { $"idn_{tableName}" }); 中的代码 - 如果您使用该示例,基本上您需要添加该服务。寻求完全理解Startup.cs 类的所有部分以及它们如何协同工作。一旦我理解了这一点,它就会让 .Net core 在这种情况下更容易理解

标签: c# asp.net-core


【解决方案1】:

不要与 Microsoft.IdentityIdentityServer4 混淆。您看到的带有 Microsoft Identity 前缀“AspNet”的表。

您可以使用 DbContext 的“OnModelCreating”方法自定义您想要的方式。 以下链接包含有关您可以使用 Microsoft Identity 执行哪些操作的完整信息。 Identity model customization in ASP.NET Core

例如。下面是来自上述链接的一些示例代码,用于自定义表名

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>(b =>
    {
        b.ToTable("MyUsers");
    });

    modelBuilder.Entity<IdentityUserClaim<string>>(b =>
    {
        b.ToTable("MyUserClaims");
    });

    modelBuilder.Entity<IdentityUserLogin<string>>(b =>
    {
        b.ToTable("MyUserLogins");
    });

    modelBuilder.Entity<IdentityUserToken<string>>(b =>
    {
        b.ToTable("MyUserTokens");
    });

    modelBuilder.Entity<IdentityRole>(b =>
    {
        b.ToTable("MyRoles");
    });

    modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
    {
        b.ToTable("MyRoleClaims");
    });

    modelBuilder.Entity<IdentityUserRole<string>>(b =>
    {
        b.ToTable("MyUserRoles");
    });
}

【讨论】:

  • 哇,你是冠军!所以基本上 microsoft.identity 只是一个自定义身份服务器的上下文类吧?
  • 可能是为了理解,可以假设IdentityServer实现是Microsoft Identity的扩展。
猜你喜欢
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 2020-07-04
  • 2023-03-15
  • 1970-01-01
  • 2021-10-27
  • 2017-09-13
  • 2017-04-20
相关资源
最近更新 更多