title: ASP .Net Core Access the DistributedRedisCache in the JWTBearerOptions.Events OnTokenValidated Event
body: <p>I have an ASP .Net Core API Project. In this project I am using JWTBearer Authentication. I am also using the AddDistributedRedisCache feature of the .Net Core Dependency Injection. (Both shown below)</p>
  
  <p>We have a need to blacklist the tokens on occasion (Admin user removing rights, logout, etc) so that these can take immediate effect. Essentially forcing a user to log back in before the next call can be made.</p>
  
  <p>We are adding the JWT Tokens to the redis cache as well as removing them from the client side cache on logout. But a user could (in theory) store the JWT token, and still gain access until the token expires, unless we intercept the call and check it against the blacklist.</p>
  
  <p>How can I access the distributed cache object in the "OnTokenValidated" event in the code below? Do I have to manually create a new connection each time? We are only checking valid tokens, as that will stop invalid requests from even being checked against the blacklist.</p>
  
  <p>Bearer Token Config:</p>
  
  <pre><code>services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  .AddJwtBearer(options =&gt;
                  {
                      options.TokenValidationParameters = new TokenValidationParameters
                      {
                          ValidateIssuer = true,
                          ValidateAudience = true,
                          ValidateLifetime = true,
                          ValidateIssuerSigningKey = true,
                          ValidIssuer = "localhost:5000",
                          ValidAudience = "localhost:5000",
                          IssuerSigningKey = new SymmetricSecurityKey(
                              Encoding.UTF8.GetBytes(Configuration.GetValue&lt;string&gt;("SigningKey"))),
                      };
                      options.Events = new JwtBearerEvents
                          {
                              OnTokenValidated = context =&gt; {
                                  //context.Fail("User has been logged out");
                                  return Task.CompletedTask;
                              }
                          };
                  });
  </code></pre>
  
  <p>Redis Cache Config:</p>
  
  <pre><code>services.AddDistributedRedisCache(option =&gt;
              {
                  option.Configuration = Configuration.GetValue&lt;string&gt;("RedisCacheAddress");
                  option.InstanceName = Configuration.GetValue&lt;string&gt;("RedisCacheInstance");
              });
  </code></pre>