Using Custom Entities with OpenIddict

0

I'm trying to extend the entities used by OpenIddict based on the example provided here: https://github.com/openiddict/openiddict-core/issues/360#issuecomment-280268525

This includes creating entity classes extending those given by 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>
{

}

Updating AddDbContext:

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

And a modified version of the call to register the services, to match the newer API:

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

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

Everything appears fine up to this point. Then, as part of application startup, I check for an existing client, and create it if it's not found:

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);
}

As noted by the "Throws Here" comment, an exception is thrown which is a 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'.

It appears somewhere along the way, it's trying to query the old tables instead of the newer ones that I'm specifying. The only things on the callstack for OpenIddict between my seed method, and the SQL exception are OpenIddict.Core.OpenIddictApplicationManager`1.FindByClientIdAsync(String identifier, CancellationToken cancellationToken) and OpenIddict.Core.OpenIddictApplicationCache`1.<>c__DisplayClass6_0.<<FindByClientIdAsync>g__ExecuteAsync|0>d.MoveNext()

asp.net-core
entity-framework-core
openiddict
asked on Stack Overflow May 7, 2019 by Matt Sieker

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0