Path: redb-route/redb.Route.WebSocket/Extensions/ServiceCollectionExtensions.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.WebSocket/Extensions/ServiceCollectionExtensions.cs ---- using System.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using redb.Route.Extensions; using redb.Route.Http; namespace redb.Route.WebSocket; /// /// Extension methods for registering the WebSocket component with dependency injection. /// public static class ServiceCollectionExtensions { /// /// Registers the WS and WSS components with the route context. /// public static IServiceCollection AddRedbRouteWebSocket(this IServiceCollection services) => AddRedbRouteWebSocket(services, null); /// /// Registers the WS and WSS components and lets the host authenticate incoming connections. /// /// services.AddRedbRouteWebSocket(o => /// o.Authenticate = async ctx => await myJwtValidator.ValidateAsync( /// ctx.Request.Query["access_token"])); /// /// public static IServiceCollection AddRedbRouteWebSocket( this IServiceCollection services, Action? configure) { var registration = new WsRegistrationOptions(); configure?.Invoke(registration); // ws endpoints multiplex onto the same Kestrel as HTTP/gRPC/SOAP/AS2 (idempotent call). services.AddRedbRouteHttpHosting(); services.AddSingleton(); services.AddSingleton(); // IRouteContextConfigurator is applied by RouteHostedService at startup -- // the correct registration hook (a lazy marker singleton never fires). services.AddRouteContextConfigurator((sp, context) => { var serverManager = sp.GetRequiredService(); var ws = sp.GetRequiredService(); ws.ServerManager = serverManager; ws.Authenticate = registration.Authenticate; context.AddComponent(ws); var wss = sp.GetRequiredService(); wss.ServerManager = serverManager; wss.Authenticate = registration.Authenticate; context.AddComponent(wss); }); return services; } } /// /// Host-supplied configuration for WebSocket consumers. The route host is a generic host, not an /// ASP.NET application, so authentication arrives as a delegate the host writes rather than /// through an authentication stack the connector would have to depend on. /// public sealed class WsRegistrationOptions { /// /// Authenticates a WebSocket handshake. Returns the principal to attach to the request (its /// NameIdentifier claim reaches the route as the redbWs.UserId header), or null /// to reject the upgrade with 401. /// /// Browsers cannot set headers on a WebSocket handshake, so a token normally arrives in the /// access_token query parameter: read it from there and from the Authorization header. /// /// /// The principal is also put on every exchange the socket produces (ExchangePrincipal). Build its /// identity with an authentication type (new ClaimsIdentity(claims, "Bearer")): code that reads /// the principal treats an identity that is not authenticated as anonymous. /// /// public Func>? Authenticate { get; set; } }