【发布时间】:2021-06-01 13:03:24
【问题描述】:
我知道以前有人问过这个问题。但在那之后的大约 8 年里,SignalR 发生了很大变化。
那么有人知道如何从 SignalR 集线器获取客户端的 IP 吗?
我正在使用 SignalR 在不同服务器上的两个 .net 核心应用程序之间进行通信,因此没有 HTTP 请求或为网站提供服务或类似的东西。
【问题讨论】:
标签: c# asp.net-core .net-core signalr
我知道以前有人问过这个问题。但在那之后的大约 8 年里,SignalR 发生了很大变化。
那么有人知道如何从 SignalR 集线器获取客户端的 IP 吗?
我正在使用 SignalR 在不同服务器上的两个 .net 核心应用程序之间进行通信,因此没有 HTTP 请求或为网站提供服务或类似的东西。
【问题讨论】:
标签: c# asp.net-core .net-core signalr
在您的中心内,您可以阅读 IHttpConnectionFeature 功能,例如:
using Microsoft.AspNetCore.Http.Features;
...
var feature = Context.Features.Get<IHttpConnectionFeature>();
它将返回具有以下属性的IHttpConnectionFeature 实例:
public interface IHttpConnectionFeature
{
//
// Summary:
// The unique identifier for the connection the request was received on. This is
// primarily for diagnostic purposes.
string ConnectionId { get; set; }
//
// Summary:
// The IPAddress of the client making the request. Note this may be for a proxy
// rather than the end user.
IPAddress? RemoteIpAddress { get; set; }
//
// Summary:
// The local IPAddress on which the request was received.
IPAddress? LocalIpAddress { get; set; }
//
// Summary:
// The remote port of the client making the request.
int RemotePort { get; set; }
//
// Summary:
// The local port on which the request was received.
int LocalPort { get; set; }
}
代码示例:
public override Task OnConnectedAsync()
{
var feature = Context.Features.Get<IHttpConnectionFeature>();
_logger.LogInformation("Client connected with IP {RemoteIpAddress}", feature.RemoteIpAddress);
return base.OnConnectedAsync();
}
【讨论】: