title: NetCore 3.1 Use IdentityServer4 with existing database and Users table
body: <p>I have an existing database with a table called Users (its actually an old  DotNetNuke 8.1 (like around 2016) database table structure with Users, UserRoles, etc tables).
  It does not follow the structure of the current Microsoft Identity such as (AspNetUsers, AspNetUserRoles...etc).</p>
  <p>I want to add an authentication layer into the NetCore 3.1 project that uses this database.
  I managed to scaffold the database tables into models and add a db context classes, so I can access the Users table.</p>
  <p>How can I add IdentityServer4 for Authentication with the usernames and their passwprds from the Users table.This is what I have so far:</p>
  <pre><code>public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
                
              services.AddDbContext&lt;ApplicationDbContext&gt;(option =&gt; option.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));
  
              // added for identity server
              services.AddIdentityServer()
                  .AddInMemoryApiResources(Configurations.Configuration.GetApis())
                  .AddInMemoryClients(Configurations.Configuration.GetClients())
                  .AddDeveloperSigningCredential();
  
              services.AddTransient&lt;IResourceOwnerPasswordValidator, Configurations.ResourceOwnerPasswordValidator&gt;();
              services.AddTransient&lt;IProfileService, Configurations.ProfileService&gt;();
  
              services.AddControllersWithViews();
  
            
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext dbContext)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              // added for identity server
              app.UseIdentityServer();
  
              //added
              app.UseAuthentication();
  
              app.UseAuthorization();
  
              //added
              dbContext.Database.EnsureCreated();
  
              app.UseEndpoints(endpoints =&gt;
              {
  
                  endpoints.MapControllerRoute(
                     name: &quot;default&quot;,
                     pattern: &quot;{controller=Test}/{action=Index}/{id?}&quot;);
              });
          }
      }
  </code></pre>
  <p>The Configuration class for IdentityServer4:</p>
  <pre><code>public static class Configuration
      {
          public static IEnumerable&lt;ApiResource&gt; GetApis() =&gt;
              new List&lt;ApiResource&gt; 
              { 
                  new ApiResource(&quot;ApiOne&quot;)  // registering one api with this name
              };
  
          public static IEnumerable&lt;Client&gt; GetClients() =&gt;    // define a client that will consume above api
              new List&lt;Client&gt;
              {
                  new Client
                  {
                      ClientId = &quot;resClient&quot;,
                      ClientSecrets = { new Secret(&quot;topsecret&quot;.ToSha256()) },   // make client secret more complex for production, can be made to expire
  
  
                      AllowedGrantTypes = GrantTypes.ClientCredentials,    // how client will request the access token
  
                      //define what the access token will be allowed to be used for, the scopes
                      AllowedScopes = { &quot;ApiOne&quot; }     // this client will be allowed to access Api One
                  }
              };
  
  
      }
  </code></pre>
  <p>The ProfileService class:</p>
  <pre><code>public class ProfileService : IProfileService
      {
          public Task GetProfileDataAsync(ProfileDataRequestContext context)
          {
              context.IssuedClaims = context.Subject.Claims.ToList();
              return Task.FromResult(0);
          }
  
          public Task IsActiveAsync(IsActiveContext context)
          {
              return Task.FromResult(0);
          }
      }
  </code></pre>
  <p>I try with a username and pass from the db (not hashed, its just for testing) but the return is always:</p>
  <blockquote>
  <p>&quot;unauthorized_client&quot;</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/2asxR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2asxR.png" alt="enter image description here" /></a></p>