title: IdentityServer4 How to register user and manage resources
body: <p>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.</p>
  
  <p>I have already been through the IdentityServer4 docs and started with InMemoryUsers which I now changed to <strong>IdentityUser</strong> with <strong>Entity Framework Core</strong> </p>
  
  <pre><code>        // Adds Identity - configure identity server
      services.AddIdentityServer()
          // .AddSigningCredential(key, StoreLocation.LocalMachine, NameType.Thumbprint)
          .AddTemporarySigningCredential()
          .AddConfigurationStore(builder =&gt; builder.UseSqlServer(connectionString, options =&gt; options.MigrationsAssembly(migrationsAssembly)))
          .AddOperationalStore(builder =&gt; builder.UseSqlServer(connectionString, options =&gt; options.MigrationsAssembly(migrationsAssembly)))
          .AddAspNetIdentity&lt;IdentityUser&gt;();
  </code></pre>
  
  <p>So I created like in the thread <a href="https://stackoverflow.com/questions/42450840/user-registration-process-with-identityserver4" title="User Registration Process with IdentityServer4">User Registration Process with IdentityServer4</a> already mentioned, three different projects.</p>
  
  <ul>
  <li><strong>Identity Provider</strong> = <em>Asp.Net Identity project (could be company website with user registration)</em></li>
  <li><strong>Identity Server</strong> = <em>Asp.Net project containing the identity server middleware</em></li>
  <li><strong>A protected API</strong> = <em>Web Api</em></li>
  </ul>
  
  <p>I also have to mention that I am using <strong>Entity Framework Core 1.1.1</strong>.</p>
  
  <p>But now the question came out, how to handle the database access from the different projects? 
  The <strong>Identity Provider</strong> and the <strong>Identity Server</strong> are currently using the same database. Both projects have migration enabled, but could that be right?
  <a href="https://i.stack.imgur.com/73lPZ.png" rel="nofollow noreferrer">IdentityProvider with ApplicationDBContext and IdentityServer=OAuthService with ApplicationDBContext, both with Migration enabled</a></p>
  
  <ol>
  <li>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)</li>
  <li>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?</li>
  </ol>
  
  <p>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:
  <a href="https://i.stack.imgur.com/TTOgv.png" rel="nofollow noreferrer">IdentityServer database with Asp.Net Identities</a></p>
  
  <p>The following code will be executed within the <strong>Startup.cs</strong> of the SocialNet.OAuthService project.</p>
  
  <pre><code>    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&lt;ConfigurationDbContext&gt;();
  
                  // 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&lt;UserManager&lt;IdentityUser&gt;&gt;();
                  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&lt;string&gt;
                              {
                                  UserId = identityUser.Id,
                                  ClaimType = claim.Type,
                                  ClaimValue = claim.Value
                              });
                          }
  
                          userManager.CreateAsync(identityUser, "Password123!").Wait();
                      }
                  }
              }
          }
  </code></pre>
  
  <p>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</p>