How to generate migrations for this new ApiAuthorizationDbContext?
Since I can't get access to IOptions<OperationalStoreOptions> from IDesignTimeDbContextFactory
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 21cb7455-3305-9316-5440-bdc22947a82d
- Version Independent ID: 137d4b94-7b26-3911-b22d-42c754a95fc1
- Content: Introduction to authentication for Single Page Apps on ASP.NET Core
- Content Source: aspnetcore/security/authentication/identity-api-authorization.md
- Product: aspnet-core
- Technology: aspnetcore-security
- GitHub Login: @javiercn
- Microsoft Alias: scaddie
There's nothing special to using ApiAuthorizationDbContext. IDesignTimeDbContextFactory. Create a service collection. Initialize your container as you would do in your app. Resolve the required services.
Alternatively, simply avoid using the class, and have your DbContext extend IdentityDbContext and implement the appropriate interfaces as https://github.com/aspnet/AspNetCore/blob/master/src/Identity/ApiAuthorization.IdentityServer/src/Data/ApiAuthorizationDbContext.cs does.
i guess it was my local issue with tooling version. thx!
It look like the last time it worked for me was 3.0.0-preview7.19362.6. I get an object reference error on the following:
private static Assembly GetApplicationAssembly(IdentityBuilder builder)
{
// Whis is the same logic that MVC follows to find the application assembly.
var environment = builder.Services.Where(d => d.ServiceType == typeof(IWebHostEnvironment)).ToArray();
var applicationName = ((IWebHostEnvironment)environment.LastOrDefault()?.ImplementationInstance)
.ApplicationName;
Below is my design code. My AuthDbContext is extracted out into a library project.
public class DesignTimeAuthDbContextFactory : IDesignTimeDbContextFactory<AuthDbContext>
{
private const string CONN_STRING = "Server=(local);Database=MyDb;Trusted_Connection=True;MultipleActiveResultSets=true";
public AuthDbContext CreateDbContext(string[] args)
{
Debugger.Launch();
IServiceCollection services = new ServiceCollection();
var cAuth = new AuthConfiguration()
{
ConnectionString = CONN_STRING
};
services.AddSingleton<AuthConfiguration>(cAuth);
services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(cAuth.ConnectionString, x => x.MigrationsHistoryTable(HistoryRepository.DefaultTableName, AuthConfiguration._SCHEMA)));
services
.AddDefaultIdentity<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, AuthDbContext>();
var context = services.BuildServiceProvider().GetService<AuthDbContext>();
return context;
}
Fixed
public class DesignTimeAuthDbContextFactory : IDesignTimeDbContextFactory<AuthDbContext>
{
private const string CONN_STRING = "Server=(local);Database=MyDb;Trusted_Connection=True;MultipleActiveResultSets=true";
public AuthDbContext CreateDbContext(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(CONN_STRING));
services.Configure<IdentityServer4.EntityFramework.Options.OperationalStoreOptions>(x =>{});
var context = services.BuildServiceProvider().GetService<AuthDbContext>();
return context;
}
}