title: Accessing DbContext from custom Class .Net Core
body: <p>I need to access my DbContext from one handler class which is instantiated in the configure method of <code>Startup.cs</code> class. How Can Instantiate my handler class in order to use the db context registered with the dependency injection container in <code>Startup.ConfigureServices</code> method.</p>
  
  <p>This is my code:</p>
  
  <p>Startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
  
      var connection = @"Server=MyDb;Initial Catalog=MYDB;Persist Security Info=True; Integrated Security=SSPI;";
      services.AddDbContext&lt;iProfiler_ControlsContext&gt;(options =&gt; options.UseSqlServer(connection));
  
      //.........
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
      //.............
  
      options.SecurityTokenValidators.Add(new MyTokenHandler(MY INSTANCE OF DBCONTEXT HERE));
      app.UseJwtBearerAuthentication(options);
  
      //..............
  
  }
  </code></pre>
  
  <p>Handler Class:</p>
  
  <pre><code>internal class MyTokenHandler : ISecurityTokenValidator
  {
      private JwtSecurityTokenHandler _tokenHandler;
      private iProfiler_ControlsContext _context;
  
      public MyTokenHandler(iProfiler_ControlsContext context)
      {
          _tokenHandler = new JwtSecurityTokenHandler();
          _context = context;
      }
  
    public ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
     {
  
      var principal = _tokenHandler.ValidateToken(securityToken, validationParameters, out validatedToken);
  
      var tblVerificationPortalTimeStamps = _context.TblVerificationPortalTimeStamps.ToList();   
  
     //......         
  
     }
  }
  </code></pre>