Skip to content

Instantly share code, notes, and snippets.

@tariknz
Created April 29, 2019 04:00
Show Gist options
  • Select an option

  • Save tariknz/4ab1819f19fe5d3bb14c6c020bec4818 to your computer and use it in GitHub Desktop.

Select an option

Save tariknz/4ab1819f19fe5d3bb14c6c020bec4818 to your computer and use it in GitHub Desktop.
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