title: How to validate JWT during websocket request. .net core
body: <p>I am working on a small .net core app that uses JWT authentication and websockets.</p>
  
  <p>I have succesfully implemented generating and validating tokens for standard web api controllers. However I also want to validate the token for a <code>WebSocket</code> request which of course won't work with the <code>[Authorize]</code> attribute.</p>
  
  <p>I have setup my middleware pipeline like this:</p>
  
  <pre><code>app.UseWebSockets();
  app.Use(async (http, next) =&gt; {
        if (http.WebSockets.IsWebSocketRequest == false) {
            await next();
            return;
        }
        /// Handle websocket request here. How to check if token is valid?
  });
  
  // secretKey contains a secret passphrase only your server knows
  var secretKey = .....;
  var signKey = new SigningCredentials (
      new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)),
      SecurityAlgorithms.HmacSha256
  );
  
  var tokenValidationParameters = new TokenValidationParameters {
      ValidateIssuer = false,
      ValidateAudience = false,
  
      // The signing key must match!
      ValidateIssuerSigningKey = true,
      IssuerSigningKey = signKey.Key,
  
      // Validate the token expiry
      ValidateLifetime = true,
  
      // If you want to allow a certain amount of clock drift, set that here:
      ClockSkew = TimeSpan.FromMinutes(1),
  };
  
  
  app.UseJwtBearerAuthentication(new JwtBearerOptions {
      AutomaticAuthenticate = true,
      AutomaticChallenge = true,
      TokenValidationParameters = tokenValidationParameters
  });
  </code></pre>