OpenIddict - 使用自定义键时刷新令牌流导致实体错误

OpenIddict - Refresh token flow causes entity error when using custom key

测试刷新令牌流时,在使用为 UseOpenIddict 方法(在本例中为 )指定自定义密钥的重载签名时出现以下错误。

InvalidOperationException: 未找到实体类型 'OpenIddictAuthorization'。确保实体类型已添加到模型中。

有趣的是,如果我不使用重载方法使用 int 作为主键,它会正常工作并且我会收到刷新令牌。只有当我使用重载时才会收到此错误。

这里是启动时的上下文声明

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDbContext<RouteManagerContext>(options =>
            {
                options.UseSqlServer(AppSettings.RouteManagerContext);
                options.UseOpenIddict<int>();
            });
            services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<RouteManagerContext>().AddDefaultTokenProviders();
            services.Configure<IdentityOptions>(options =>
            {
                options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
                options.ClaimsIdentity.UserIdClaimType = OpenIdConnectConstants.Claims.Subject;
                options.ClaimsIdentity.RoleClaimType = OpenIdConnectConstants.Claims.Role;
            });
            services.AddOpenIddict(options =>
            {
                options.AddEntityFrameworkCoreStores<RouteManagerContext>();
                options.AddMvcBinders();
                options.EnableTokenEndpoint("/connect/token");
                options.AllowPasswordFlow()
                    .AllowRefreshTokenFlow()
                    .SetAccessTokenLifetime(TimeSpan.FromMinutes(1))
                    .SetRefreshTokenLifetime(TimeSpan.FromMinutes(20160))
                options.DisableHttpsRequirement();
            });
            services.AddAuthentication()
                .AddOAuthValidation()
                .AddFacebook(o => { o.ClientId = AppSettings.FacebookAppID; o.ClientSecret = AppSettings.FacebookAppSecret; });
            services.AddDocumentation(AppSettings);
        }

这是我的背景

public class RouteManagerContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
    public RouteManagerContext(DbContextOptions<RouteManagerContext> options) : base(options)  { }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

应用程序似乎配置正确,因为我在数据库中拥有 openiddict 需要的所有表:应用程序、授权、令牌等...

所有示例的配置方式似乎都相同。 有什么想法吗?

通过调用 options.UseOpenIddict<int>();,您要求 Entity Framework Core 使用默认的 OpenIddict 实体,但使用自定义键类型(int 而不是 string)。

然而,您还在使用 services.AddOpenIddict(),它使用默认实体和默认密钥类型配置 OpenIddict。当 Entity Framework 核心存储被 OpenIddict 调用时,预期的实体无法在上下文中找到,因为它们的通用定义不同。

要解决不一致问题,请使用 services.AddOpenIddict<int>()

我遇到了同样的错误,但使用的是 Guig 密钥 options.UseOpenIddict<Guid>();

我使用 OpenIddict 2.0 并且方法services.AddOpenIddict<Guid>()不存在

我解决了这个错误,使用这个代码:

 services.AddOpenIddict()
          .AddCore(options =>
          {
              options.UseEntityFrameworkCore()
              .UseDbContext<ApplicationDbContext>()
              .ReplaceDefaultEntities<Guid>();
          })

          .AddServer(options =>...) //here more options

我还使用 Guid 作为身份实体的键

public class ApplicationDbContext : IdentityDbContext<IdentityUser<Guid>, IdentityRole<Guid>, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }