Created
April 29, 2019 04:00
-
-
Save tariknz/4ab1819f19fe5d3bb14c6c020bec4818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.Http; | |
| namespace AdminService.Middleware | |
| { | |
| public class WebSocketsMiddleware | |
| { | |
| private readonly RequestDelegate _next; | |
| public WebSocketsMiddleware(RequestDelegate next) | |
| { | |
| _next = next; | |
| } | |
| public async Task Invoke(HttpContext httpContext) | |
| { | |
| var request = httpContext.Request; | |
| // web sockets cannot pass headers so we must take the access token from query param and | |
| // add it to the header before authentication middleware runs | |
| if (request.Path.StartsWithSegments("/hub", StringComparison.OrdinalIgnoreCase) && | |
| request.Query.TryGetValue("access_token", out var accessToken)) | |
| { | |
| request.Headers.Add("Authorization", $"Bearer {accessToken}"); | |
| } | |
| await _next(httpContext); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment