mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-13 12:18:08 +00:00
Phase 3: Pass authentication token to SignalR connection URL
- Modified main.js to pass authToken to SignalRBridge constructor - Updated signalr-bridge.js to append token to hub URL - Removed skip logic for negotiate endpoint in middleware - SignalR negotiate request now includes token for authentication - Cookie is set after successful negotiate for subsequent requests This enables SignalR connections to authenticate using the same token-based mechanism as HTTP requests, completing the authentication flow for both Blazor pages and SignalR hub communication.
This commit is contained in:
@@ -24,13 +24,6 @@ namespace ElectronNET.AspNet.Middleware
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
// Skip authentication for SignalR negotiation (will be handled by cookie)
|
||||
if (context.Request.Path.StartsWithSegments("/electron-hub/negotiate"))
|
||||
{
|
||||
await _next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if authentication cookie exists
|
||||
var authCookie = context.Request.Cookies[AuthCookieName];
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@ const safeConsole = {
|
||||
};
|
||||
|
||||
class SignalRBridge {
|
||||
constructor(hubUrl) {
|
||||
constructor(hubUrl, authToken) {
|
||||
this.hubUrl = hubUrl;
|
||||
this.authToken = authToken;
|
||||
this.connection = null;
|
||||
this.isConnected = false;
|
||||
this.eventHandlers = new Map(); // For socket.io-style .on() handlers
|
||||
@@ -39,8 +40,11 @@ class SignalRBridge {
|
||||
}
|
||||
|
||||
async connect() {
|
||||
// Append authentication token to the SignalR connection URL
|
||||
const connectionUrl = this.authToken ? `${this.hubUrl}?token=${this.authToken}` : this.hubUrl;
|
||||
|
||||
this.connection = new signalR.HubConnectionBuilder()
|
||||
.withUrl(this.hubUrl)
|
||||
.withUrl(connectionUrl)
|
||||
.withAutomaticReconnect()
|
||||
.configureLogging(signalR.LogLevel.Warning)
|
||||
.build();
|
||||
|
||||
@@ -445,7 +445,9 @@ function startSocketApiBridge(port) {
|
||||
async function startSignalRApiBridge(baseUrl) {
|
||||
const { SignalRBridge } = require('./api/signalr-bridge');
|
||||
const hubUrl = `${baseUrl}/electron-hub`;
|
||||
const signalRBridge = new SignalRBridge(hubUrl);
|
||||
|
||||
// Pass the authentication token to the SignalR bridge
|
||||
const signalRBridge = new SignalRBridge(hubUrl, global.authToken);
|
||||
|
||||
try {
|
||||
const connected = await signalRBridge.connect();
|
||||
|
||||
Reference in New Issue
Block a user