1.NUGET安装 Microsoft.AspNetCore.Http
2.在 startup.cs 的 ConfigureServices 中注入
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
3.获取ip
HttpContextAccessor context = new HttpContextAccessor(); var ip = context.HttpContext?.Connection.RemoteIpAddress.ToString();