前端:

引用signalr三方包
This commit is contained in:
2026-01-19 19:37:47 +08:00
parent e7dbb651a2
commit 507788f6a3
9 changed files with 274 additions and 9 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ namespace IM_API.Hubs
}
var userIdStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier);
await _messageService.SendPrivateMessageAsync(int.Parse(userIdStr),dto.ReceiverId,dto);
await Clients.Users(dto.ReceiverId.ToString()).SendAsync("ReceiveMessage", userIdStr, dto.Content);
await Clients.Users(dto.ReceiverId.ToString()).SendAsync("ReceiveMessage", dto);
return;
}
}
+8 -6
View File
@@ -44,7 +44,13 @@ namespace IM_API
policy.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin();
.AllowCredentials()
.SetIsOriginAllowed(origin =>
{
// 允许所有来自本地或特定网段的请求
var host = new Uri(origin).Host;
return host == "localhost" || host.StartsWith("192.168.");
});
});
});
//凭证处理
@@ -86,11 +92,7 @@ namespace IM_API
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
options.Events = new JwtBearerEvents
{
},
OnAuthenticationFailed = context =>
{
Console.WriteLine("Authentication failed: " + context.Exception.Message);