在 Asp.net (core) .net 5 中,在 SignalR 端,我們如何列出(接收)客戶端 IP 并阻止或允許某些 IP 的連接和 IP 請求?
在我的研究中,我發現只提到了 Azure SingalR。
我們如何通過在應用程式中編碼來告訴 SignalR Hub 請求客戶端 IP 并接受(白名單)或阻止(黑名單)來自特定 IP 的公共連接請求?
是否可以 ?
uj5u.com熱心網友回復:
在 SignalR 中,您可以使用 HttpContext 獲取用戶的 ip。
public class AppHub : Hub
{
public string Ip => Context.GetHttpContext().Connection.RemoteIpAddress.ToString();
public override async Task OnConnectedAsync()
{
if (Ip == "::1")
{
await Clients.All.SendAsync("ReceiveMessage", "Server", "Welcome to the server!");
}
else
{
throw new Exception("You are not allowed to connect to this server!");
}
await base.OnConnectedAsync();
}
}
另一種解決方案是創建一個集線器過濾器,您可以在此處找到檔案: https ://docs.microsoft.com/en-us/aspnet/core/signalr/hub-filters?view=aspnetcore-6.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/414084.html
標籤:
上一篇:從IdentityServer4.AspNetIdentity3.x遷移到4.x
下一篇:.NETCore3.1-SAP連接器-無法加載型別“System.ServiceModel.Activation.VirtualPathExtension”
