title: JWT & Simple Injector. Set connectionstring OnTokenValidated
body: <p>I am new to this kind of concepts so i am kinda stuck on this (explanation of what i aim for a little bellow).</p>
  
  <p>I have the following code in a rest web api:</p>
  
  <p>UnitOfWorkProvider.cs</p>
  
  <pre><code>    public class UnitOfWorkProvider : IUnitOfWorkProvider
  {
      private readonly Func&lt;AppDbContext&gt; getDbContext;
  
      public UnitOfWorkProvider(Func&lt;AppDbContext&gt; getDbContext)
      {
          this.getDbContext = getDbContext;
      }
  
      public IUnitOfWork Get()
      {
          return new AppUnitOfWork(this.getDbContext());
      }
  }
  </code></pre>
  
  <p>SimpleInjectorConfiguration.cs</p>
  
  <pre><code>        internal static void Configure(this Container container, IConfigurationRoot configuration, IApplicationBuilder app, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor, IMemoryCache memoryCache)
      {
          InitializeInjectorContainer(container, configuration, app, loggerFactory, httpContextAccessor, memoryCache);
      }
  
      private static void InitializeInjectorContainer(Container container, IConfigurationRoot configuration, IApplicationBuilder app, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor, IMemoryCache memoryCache)
      {
          container.Register&lt;IUnitOfWorkProvider&gt;(() =&gt; new UnitOfWorkProvider(() =&gt; new AppDbContext(configuration.GetConnectionString("AppDbContext"))), Lifestyle.Scoped);
  
          ...
      }
  </code></pre>
  
  <p>that is called on Startup.cs</p>
  
  <pre><code>        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor, IMemoryCache memoryCache)
      {
          // Configure and register Simple Injector
          simpleInjectorContainer.Configure(configuration, app, loggerFactory, httpContextAccessor, memoryCache);
          ...
      }
  </code></pre>
  
  <p>What i am for is register the IUnitOfWorkProvider like this in SimpleInjectorConfiguration.cs:</p>
  
  <pre><code>container.Register&lt;IUnitOfWorkProvider&gt;();
  </code></pre>
  
  <p>and instantiate it later on JWT OnTokenValidated event when i get the database name from the claims in request:</p>
  
  <pre><code>            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
              .AddJwtBearer(options =&gt;
              {
                  options.Events = new JwtBearerEvents()
                  {
                      OnTokenValidated = context =&gt;
                      {
                          // get database name from claims in request
  
                          // something like this to set the context. this doesnt work.
                          container.GetInstance&lt;IUnitOfWorkProvider&gt;(() =&gt; new UnitOfWorkProvider(() =&gt; new AppDbContext(SetConnectionString(dbNameFromClaim))), Lifestyle.Scoped);
                          return Task.CompletedTask;
                      }
                  };
              });
  </code></pre>
  
  <p>Many thanks to whom might help me! :)</p>