I am planning to use the IdentityServer4 to handle authentication/authorization for multiple projects (every project will have its own WebAPI). So user maybe have to register once and get a flag or role for which product/api they have access to.
I have already been through the IdentityServer4 docs and started with InMemoryUsers which I now changed to IdentityUser with Entity Framework Core
// Adds Identity - configure identity server
services.AddIdentityServer()
// .AddSigningCredential(key, StoreLocation.LocalMachine, NameType.Thumbprint)
.AddTemporarySigningCredential()
.AddConfigurationStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly)))
.AddOperationalStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly)))
.AddAspNetIdentity<IdentityUser>();
So I created like in the thread User Registration Process with IdentityServer4 already mentioned, three different projects.
- Identity Provider = Asp.Net Identity project (could be company website with user registration)
- Identity Server = Asp.Net project containing the identity server middleware
- A protected API = Web Api
I also have to mention that I am using Entity Framework Core 1.1.1.
But now the question came out, how to handle the database access from the different projects? The Identity Provider and the Identity Server are currently using the same database. Both projects have migration enabled, but could that be right? IdentityProvider with ApplicationDBContext and IdentityServer=OAuthService with ApplicationDBContext, both with Migration enabled
- So my first question is, isn´t that a problem to handle two migrations to the same data source? (It seems that in Entity Framework Core, wont be executed automatically -which is good- and migrations have to be executed manually)
- Which way is recommended to handle the Clients, API resource, Claims, Scopes, etc. for the IdentityServer4? Shall I create an additional project, which also use the same database just to manage the configuration data?
So I am not sure how to handle database access to one database from different Asp.Net projects with Entity Framework Core. My database currently looks like that: IdentityServer database with Asp.Net Identities
The following code will be executed within the Startup.cs of the SocialNet.OAuthService project.
private void InitializeDbTestData(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService().CreateScope())
{
// create identity server EF structures
serviceScope.ServiceProvider.GetRequiredService().Database.Migrate();
serviceScope.ServiceProvider.GetRequiredService().Database.Migrate();
serviceScope.ServiceProvider.GetRequiredService().Database.Migrate();
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
// add dummy/example data
if (!context.Clients.Any())
{
foreach (var client in ExampleConfig.GetClients())
context.Clients.Add(client.ToEntity());
context.SaveChanges();
}
if (!context.IdentityResources.Any())
{
foreach (var resource in ExampleConfig.GetIdentityResource())
context.IdentityResources.Add(resource.ToEntity());
context.SaveChanges();
}
if (!context.ApiResources.Any())
{
foreach (var apiResource in ExampleConfig.GetApiResources())
context.ApiResources.Add(apiResource.ToEntity());
context.SaveChanges();
}
var userManager = serviceScope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
if (!userManager.Users.Any())
{
foreach (var testUser in ExampleConfig.GetUsers())
{
var identityUser = new IdentityUser(testUser.Username)
{
Id = testUser.SubjectId,
UserName = testUser.Username,
Email = testUser.Username,
EmailConfirmed = false
};
foreach (var claim in testUser.Claims)
{
identityUser.Claims.Add(new IdentityUserClaim<string>
{
UserId = identityUser.Id,
ClaimType = claim.Type,
ClaimValue = claim.Value
});
}
userManager.CreateAsync(identityUser, "Password123!").Wait();
}
}
}
}
I hope I could describe my problem comprehensible and I hope the formatting is conform to the guidelines. I am really thankful for your help. Best Regards Michael