You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7+Identity Server升级Duende包后get_ServerSideSessions缺失求助

修复方案:.NET7 + Duende Identity Server 6.2.0 兼容性错误

问题根源

该错误是由于Microsoft.AspNetCore.ApiAuthorization.IdentityServer(微软封装的Identity Server适配包)与Duende.IdentityServer.EntityFramework/Storage 6.2.0之间的接口不兼容导致的。Duende 6.2.0在IIdentityServerDbContext接口中新增了ServerSideSessions属性,但微软的ApiAuthorizationDbContext尚未实现该属性,从而触发反射加载异常。

具体修复方案

方案1:自定义实现ApiAuthorizationDbContext

替换微软的ApiAuthorizationDbContext为自定义实现,手动实现IIdentityServerDbContext接口的所有属性(包括新增的ServerSideSessions):

  1. 创建自定义DbContext类:
using Duende.IdentityServer.EntityFramework.Entities;
using Duende.IdentityServer.EntityFramework.Interfaces;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class CustomApiAuthorizationDbContext<TUser> : IdentityDbContext<TUser>, IIdentityServerDbContext
    where TUser : IdentityUser
{
    public CustomApiAuthorizationDbContext(DbContextOptions<CustomApiAuthorizationDbContext<TUser>> options)
        : base(options)
    {
    }

    // 实现IIdentityServerDbContext要求的所有DbSet
    public DbSet<Client> Clients { get; set; }
    public DbSet<IdentityResource> IdentityResources { get; set; }
    public DbSet<ApiResource> ApiResources { get; set; }
    public DbSet<ApiScope> ApiScopes { get; set; }
    public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
    public DbSet<PersistedGrant> PersistedGrants { get; set; }
    // 新增ServerSideSessions属性实现
    public DbSet<ServerSideSession> ServerSideSessions { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // 应用Duende Identity Server的模型配置
        builder.ApplyConfigurationsFromAssembly(typeof(CustomApiAuthorizationDbContext<TUser>).Assembly);
    }
}
  1. 在Program.cs中替换DbContext注册:
// 注册自定义DbContext
builder.Services.AddDbContext<CustomApiAuthorizationDbContext<IdentityUser>>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// 配置Identity Server使用自定义DbContext
builder.Services.AddIdentityServer()
    .AddAspNetIdentity<IdentityUser>()
    .AddConfigurationStore(options =>
    {
        options.ConfigureDbContext = b =>
            b.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
                sql => sql.MigrationsAssembly(typeof(CustomApiAuthorizationDbContext<IdentityUser>).Assembly.FullName));
    })
    .AddOperationalStore(options =>
    {
        options.ConfigureDbContext = b =>
            b.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
                sql => sql.MigrationsAssembly(typeof(CustomApiAuthorizationDbContext<IdentityUser>).Assembly.FullName));
    });
  1. 执行数据库迁移:
dotnet ef migrations add AddServerSideSessions -c CustomApiAuthorizationDbContext
dotnet ef database update

方案2:升级微软适配包到兼容版本

检查并升级Microsoft.AspNetCore.ApiAuthorization.IdentityServer至与Duende 6.2.0兼容的版本(需确保该版本已实现ServerSideSessions属性)。若微软已发布适配.NET7 + Duende 6.2.0的版本,直接更新NuGet包即可解决问题。

方案3:移除微软适配包,使用纯Duende Identity Server

如果项目无需依赖微软的ApiAuthorization封装,可以完全切换到Duende Identity Server原生实现:

  • 卸载Microsoft.AspNetCore.ApiAuthorization.IdentityServer NuGet包
  • 按照Duende官方文档配置Identity Server的DbContext、客户端、资源等
  • 调整Razor Pages的授权逻辑,使用Duende原生的授权中间件

验证步骤

  1. 清理项目的bin和obj文件夹
  2. 重新生成解决方案
  3. 启动应用,检查异常是否消失

内容的提问来源于stack exchange,提问作者Diego

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:35:24