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解决办法
基础教程推荐
猜你喜欢
- C#控制台实现飞行棋小游戏 2023-04-22
- 一个读写csv文件的C#类 2022-11-06
- unity实现动态排行榜 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# windows语音识别与朗读实例 2023-04-27
- C# 调用WebService的方法 2023-03-09
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30