C
C#2w ago
Robert

Adding middleware

Hi! I face an issue whilst using the azure serverless signalr with azure function as bindings. When Signalr is running using websockets the authorization header is null and i must use the query to retrieve the token. How can I register it in my function? this is my Program.cs
var builder = FunctionsApplication.CreateBuilder(args);

builder.Services.AddAuthentication().AddBearerToken();
builder.Services.AddAuthorization();
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
builder.Services.AddServerlessHub<Functions>();
builder.Build().Run();
var builder = FunctionsApplication.CreateBuilder(args);

builder.Services.AddAuthentication().AddBearerToken();
builder.Services.AddAuthorization();
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
builder.Services.AddServerlessHub<Functions>();
builder.Build().Run();
essentially i'd somehow need to register this, but make it work for the function
public class QueryStringAuthenticationMiddleware
{
private readonly RequestDelegate _next;

public QueryStringAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Headers["Authorization"].Count == 0)
{
var token = context.Request.Query["auth_token"];
if (!string.IsNullOrEmpty(token))
{
context.Request.Headers["Authorization"] = $"Bearer {token}";
}
}

await _next(context);
}
}
public class QueryStringAuthenticationMiddleware
{
private readonly RequestDelegate _next;

public QueryStringAuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Headers["Authorization"].Count == 0)
{
var token = context.Request.Query["auth_token"];
if (!string.IsNullOrEmpty(token))
{
context.Request.Headers["Authorization"] = $"Bearer {token}";
}
}

await _next(context);
}
}
1 Reply
Saber
Saber2w ago
Guide for running C# Azure Functions in an isolated worker process
Learn how to use the .NET isolated worker model to run your C# functions in Azure, which lets you run your functions on currently supported versions of .NET and .NET Framework.
Want results from more Discord servers?
Add your server