【问题标题】:AspnetBoilerplate IdentityServer - Configuration StoreAspnetBoilerplate IdentityServer - 配置存储
【发布时间】:2020-08-04 02:40:07
【问题描述】:

故事背景

提示:如果您熟悉 abp + postgres + IdentityServer,则可以跳过此部分,然后转到 问题

我目前正在尝试使用 AspnetBoilerplate 实现身份提供程序。

为此,我做了以下步骤:

你可以看到我的解决方案here


问题

此时我有一个功能性的身份提供者,但客户端、api 资源和身份资源都在内存中运行,你可以see

//AuthConfigurer.cs#L45

services.AddIdentityServer()
          .AddDeveloperSigningCredential()
          .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
          .AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
          .AddInMemoryClients(IdentityServerConfig.GetClients())
          .AddAbpPersistedGrants<IAbpPersistedGrantDbContext>()
          .AddAbpIdentityServer<User>();

所以现在我想把 then 放在 EF 中,为此我尝试了以下方法:

//SSODbContext.cs
using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using Coders.SSO.Authorization.Roles;
using Coders.SSO.Authorization.Users;
using Coders.SSO.MultiTenancy;
using Abp.Localization;
using Abp.IdentityServer4;
using IdentityServer4.EntityFramework.Interfaces;
using IdentityServer4.EntityFramework.Entities;
using System.Threading.Tasks;

namespace Coders.SSO.EntityFrameworkCore
{
    public class SSODbContext : AbpZeroDbContext<Tenant, Role, User, SSODbContext>, IAbpPersistedGrantDbContext, IConfigurationDbContext
    {
        /* Define a DbSet for each entity of the application */
        public DbSet<PersistedGrantEntity> PersistedGrants { get; set; }
        public DbSet<Client> Clients { get; set; }
        public DbSet<ApiResource> ApiResources { get; set; }
        public DbSet<IdentityResource> IdentityResources { get; set; }

        public SSODbContext(DbContextOptions<SSODbContext> options)
            : base(options)
        {
        }

        // add these lines to override max length of property
        // we should set max length smaller than the PostgreSQL allowed size (10485760)
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            modelBuilder.Entity<ApplicationLanguageText>()
                .Property(p => p.Value)
                .HasMaxLength(100); // any integer that is smaller than 10485760

            modelBuilder.ConfigurePersistedGrantEntity();
        }

        public Task<int> SaveChangesAsync() => base.SaveChangesAsync();
    }
}

应用了新的迁移和更新数据库,然后我将服务调用更改如下:

services.AddIdentityServer()
          .AddDeveloperSigningCredential()
          .AddConfigurationStore<SSODbContext>()
          .AddAbpPersistedGrants<IAbpPersistedGrantDbContext>()
          .AddAbpIdentityServer<User>();

但是没有用,知道如何在 AspNetBoilerplate 上设置配置存储吗?

【问题讨论】:

  • 认为您必须更具体地说明什么不起作用,您是否将一些客户端和资源添加到数据库中?
  • 这是问题所在,我无法在我的 EF 中正确设置 IdentityServer 配置存储。我需要我的应用程序使用配置存储,然后我可以使用数据库来设置我的客户端和资源。
  • 你得到什么错误?
  • 我尝试去发现文档端点/.well-known/openid-configuration,我收到以下错误InvalidOperationException: No database provider has been configured for this DbContext.
  • 您是否已应用迁移并使用表格和内容填充数据库?你能连接到数据库吗?也许创建一个测试 DBContext 并对数据库进行一些测试查询,以确保它有效。

标签: c# .net-core entity-framework-core identityserver4 aspnetboilerplate


【解决方案1】:

也许问题是你在这里使用了一个接口?

.AddAbpPersistedGrants<IAbpPersistedGrantDbContext>()

它不应该是一个具体的类型吗?为什么需要这个接口?

【解决方案2】:

我解决了这个问题,在继承自 IdentityServer Stores 接口的应用程序项目中创建自己的 Stores,例如 Scott Brady said 在他的博客(文章:创建您自己的 IdentityServer4 存储库)中。

然后我在 Startup 中添加了我的商店,例如:

services.AddIdentityServer()
    // existing registrations
    .AddClientStore<ClientStoreAppService>()
    .AddCorsPolicyService<CorsPolicyService>()
    .AddResourceStore<ResourcesStoreAppService>()
    .AddPersistedGrantStore<PersistedGrantStoreAppService>()
    .AddDeviceFlowStore<DeviceFlowStoreAppService>(); 

一步一步

  • 创建实体
  • 创建服务
  • 在每个服务中创建必要的方法
  • 在 IdentityServer 中注册服务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2013-09-04
    • 2011-08-20
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2016-02-22
    相关资源
    最近更新 更多