将自定义实体与OpenIddict一起使用

时间:2019-05-07 19:14:23

标签: asp.net-core entity-framework-core openiddict

我正在尝试根据此处提供的示例扩展OpenIddict使用的实体: https://github.com/openiddict/openiddict-core/issues/360#issuecomment-280268525

这包括创建实体类,以扩展由OpenIddict提供的实体类:

public class CustomApplication : OpenIddictApplication<Guid, CustomAuthorization, CustomToken>
{
    public string ImpersonateUser { get; set; }
}

public class CustomAuthorization : OpenIddictAuthorization<Guid, CustomApplication, CustomToken>
{

}

public class CustomScope : OpenIddictScope<Guid>
{

}

public class CustomToken : OpenIddictToken<Guid, CustomApplication, CustomAuthorization>
{

}

更新AddDbContext

services.AddDbContext<ApplicationContext>(options =>
{
    options.UseSqlServer(Configuration.GetConnectionString("ConnectionString"));
    options.UseOpenIddict<CustomApplication, CustomAuthorization, CustomScope, CustomToken, Guid>();
});

以及用于注册服务的调用的修改版本,以匹配较新的API:

services.AddOpenIddict()
    .AddCore(options =>
    {

        options.UseEntityFrameworkCore().UseDbContext<ApplicationContext>()
            .ReplaceDefaultEntities<CustomApplication, CustomAuthorization, CustomScope, CustomToken, Guid>();
    })

到目前为止,一切似乎都很好。然后,作为应用程序启动的一部分,我检查现有的客户端,如果找不到则创建它:

var manager = serviceScope.ServiceProvider.GetRequiredService<OpenIddictApplicationManager<CustomApplication>>();

if (await manager.FindByClientIdAsync("Application") == null) // Throws Here
{
    var descriptor = new OpenIddictApplicationDescriptor
    {
        ClientId = "Application",
        ClientSecret = "application-default-secret",
        DisplayName = "Application",
        RedirectUris = { new Uri("http://localhost:5000/signin-oidc")},
        Permissions = { OpenIddictConstants.Permissions.Endpoints.Authorization,
            OpenIddictConstants.Permissions.Endpoints.Logout,
            OpenIddictConstants.Permissions.Endpoints.Token,
            OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,
            OpenIddictConstants.Permissions.GrantTypes.RefreshToken,
            OpenIddictConstants.Permissions.Scopes.Email,
            OpenIddictConstants.Permissions.Scopes.Profile,
            OpenIddictConstants.Permissions.Scopes.Roles}
    };
    await manager.CreateAsync(descriptor);
}

如“在这里抛出”注释所指出的,抛出了一个异常,它是一个SqlException:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (9ms) [Parameters=[@__identifier='Application' (Size = 100)], CommandType='Text', CommandTimeout='30']
      SELECT TOP(1) [application].[Id], [application].[ClientId], [application].[ClientSecret], [application].[ConcurrencyToken], [application].[ConsentType], [application].[DisplayName], [application].[ImpersonateUser], [application].[Permissions], [application].[PostLogoutRedirectUris], [application].[Properties], [application].[RedirectUris], [application].[Type]
      FROM [OpenIddictApplications] AS [application]
      WHERE [application].[ClientId] = @__identifier
System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ImpersonateUser'.

它出现在过程中的某处,它试图查询旧表而不是我指定的新表。我的种子方法和SQL异常之间的OpenIddict调用堆栈上唯一的内容是OpenIddict.Core.OpenIddictApplicationManager`1.FindByClientIdAsync(String identifier, CancellationToken cancellationToken)OpenIddict.Core.OpenIddictApplicationCache`1.<>c__DisplayClass6_0.<<FindByClientIdAsync>g__ExecuteAsync|0>d.MoveNext()

0 个答案:

没有答案