1、获取客户端IP地址实现方法(扩展类)using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc.ModelBinding;using System.Collections.Generic;using System.Linq;namespace WebApi.Controllers{///...
1、获取客户端IP地址实现方法(扩展类)
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using System.Collections.Generic; using System.Linq; namespace WebApi.Controllers { /// <summary> /// 扩展类 /// </summary> public static class Extension { /// <summary> /// 获取客户Ip /// </summary> /// <param name="context"></param> /// <returns></returns> public static string GetClientUserIp(this HttpContext context) { var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (string.IsNullOrEmpty(ip)) { ip = context.Connection.RemoteIpAddress.ToString(); } return ip; } } }
2、解决Asp.Net Core2.0发布到Ubuntu后不能正确获取客户IP解决办法
/// <summary> /// /// </summary> /// <param name="app"></param> /// <param name="env"></param> /// <param name="loggerFactory"></param> public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { #region 解决Ubuntu Nginx 代理不能获取IP问题 app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); #endregion }
转自:https://www.jianshu.com/p/1c1ae74de056
https://www.cnblogs.com/NuoYer/p/8252443.html
沃梦达教程
本文标题为:【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法
猜你喜欢
- .NET CORE DI 依赖注入 2023-09-27
- c# 模拟线性回归的示例 2023-03-14
- Unity Shader实现模糊效果 2023-04-27
- Unity3D实现渐变颜色效果 2023-01-16
- 如何使用C# 捕获进程输出 2023-03-10
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- user32.dll 函数说明小结 2022-12-26
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- Oracle中for循环的使用方法 2023-07-04