从 SignalR Connection 获取客户端 IP
Getting client IP from SignalR Connection
我知道之前有人问过这个问题。但是从那以后的 ~8 年里,SignalR 发生了很大的变化。
那么有人知道如何从 SignalR hub 获取客户端的 IP 吗?
我正在使用 SignalR 在不同服务器上的两个 .net 核心应用程序之间进行通信,因此没有 HTTP 请求或服务于网站或类似的东西。
在您的中心内,您可以阅读 IHttpConnectionFeature
功能,例如:
using Microsoft.AspNetCore.Http.Features;
...
var feature = Context.Features.Get<IHttpConnectionFeature>();
它将 return 具有以下属性的 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();
}
我知道之前有人问过这个问题。但是从那以后的 ~8 年里,SignalR 发生了很大的变化。
那么有人知道如何从 SignalR hub 获取客户端的 IP 吗?
我正在使用 SignalR 在不同服务器上的两个 .net 核心应用程序之间进行通信,因此没有 HTTP 请求或服务于网站或类似的东西。
在您的中心内,您可以阅读 IHttpConnectionFeature
功能,例如:
using Microsoft.AspNetCore.Http.Features;
...
var feature = Context.Features.Get<IHttpConnectionFeature>();
它将 return 具有以下属性的 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();
}