SqlException:无效的对象名称'OpenIddictTokens'

时间:2017-01-29 10:20:50

标签: asp.net-core openid-connect openiddict

当我尝试登录时出现错误: 处理请求时发生未处理的异常。

  

SqlException:无效的对象名称'OpenIddictTokens'。   System.Data.SqlClient.SqlCommand + LT;> c.b__107_0(任务   结果)

     

DbUpdateException:更新条目时发生错误。看到   细节的内在例外。   Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch + d__32.MoveNext()

要确认,这是从输出窗口输出的:

  

INSERT INTO [OpenIddictTokens]([ApplicationId],[AuthorizationId],   [主题],[类型])VALUES(@ p0,@ p1,@ p2,@ p3); SELECT [Id] FROM   [OpenIddictTokens] WHERE @@ ROWCOUNT = 1 AND [Id] = scope_identity();   抛出异常:'System.Data.SqlClient.SqlException'中   Microsoft.EntityFrameworkCore.Relational.dll

我注意到数据库中没有一个OpenIddict表。有许多使用OpenIddictDbContext的例子,我使用的是but there is a new approach,但有些东西不起作用。

我的启动文件是:

public void ConfigureServices(IServiceCollection services)
{
    try
    {
        services.AddMvc();

        services.AddEntityFrameworkSqlServer();

        services.AddScoped<UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>, AppUserStore>();
        services.AddScoped<UserManager<AppUser>, AppUserManager>();
        services.AddScoped<RoleManager<AppRole>, AppRoleManager>();
        services.AddScoped<SignInManager<AppUser>, AppSignInManager>();
        services.AddScoped<RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim>, AppRoleStore>();

        var connection = Configuration["ConnectionStrings"];
        services.AddDbContext<AppDbContext>(options => {
            options.UseSqlServer(connection);
            options.UseOpenIddict<int>();
        });

        services
            .AddIdentity<AppUser, AppRole>()
            .AddUserStore<AppUserStore>()
            .AddUserManager<AppUserManager>()
            .AddRoleStore<AppRoleStore>()
            .AddRoleManager<AppRoleManager>()
            .AddSignInManager<AppSignInManager>()
            .AddDefaultTokenProviders();

        services.AddOpenIddict<int>()
            .AddEntityFrameworkCoreStores<AppDbContext>()
            .AddMvcBinders()
            .EnableTokenEndpoint("/API/authorization/token")
            .AllowPasswordFlow()
            .AllowRefreshTokenFlow()
            .DisableHttpsRequirement();

        services.AddSingleton<DbSeeder>();

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        throw;
    }
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, DbSeeder dbSeeder)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
        {
            HotModuleReplacement = true
        });
    }
    else
    {

    }

    app.UseStaticFiles();

    app.UseIdentity();

    app.UseOpenIddict();

    app.UseOAuthValidation();

    app.UseMvc();

    try
    {

        dbSeeder.SeedAsync();
    }
    catch (AggregateException e)
    {
        throw new Exception(e.ToString());
    }
}

}

和我的DbContext:

public partial class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //fix table names
        modelBuilder.Entity<AppUser>(b =>
        {
            b.ToTable("Users");
        });

        modelBuilder.Entity<AppRole>(b =>
        {
            b.ToTable("Roles");
        });

        modelBuilder.Entity<AppUserClaim>(b =>
        {
            b.ToTable("UserClaims");
        });

        modelBuilder.Entity<AppRoleClaim>(b =>
        {
            b.ToTable("RoleClaims");
        });

        modelBuilder.Entity<AppUserRole>(b =>
        {
            b.ToTable("UserRoles");
        });

        modelBuilder.Entity<AppUserLogin>(b =>
        {
            b.ToTable("UserLogins");
        });

        modelBuilder.Entity<AppUserToken>(b =>
        {
            b.ToTable("UserTokens");
        });


        //add our

我覆盖我的所有身份类(IdentityRole,IdentityRoleClaim,IdentityUser ...)以使用<int>作为TKey。

我不知道为什么没有创建OpenIdDict表。我已经删除了我的数据库,创建了新的并运行

dotnet ef migrations add "Initial" -o "Data\Migrations"
dotnet ef database update

,但只创建了身份表。

3 个答案:

答案 0 :(得分:2)

如果我调用modelBuilder.UseOpenIddict<int>()

,它会帮助(创建表格)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.UseOpenIddict<int>();

    //fix table names

答案 1 :(得分:0)

查看&#34; startup.cs&#34;中的连接字符串。 class和&#34; appsettings.json&#34;配置文件。在我的例子中是SQL Server Express实例的名称。

答案 2 :(得分:0)

在程序包管理器控制台中执行以下操作:

add-migration

然后

update-database

它应该纠正错误