.Net5 获取客户端IP地址

1、NuGet安装 Microsoft.AspNetCore.Http

2、services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

 

 3、获取

1 HttpContextAccessor context = new HttpContextAccessor();
2 var IP = context.HttpContext?.Connection.RemoteIpAddress.ToString();

 

posted @ 2021-01-20 13:45  PrintY  阅读(996)  评论(0编辑  收藏  举报