• 【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法


    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

  • 相关阅读:
    ORACLE 计算时间相减间隔
    oracle中游标详细用法
    oracle中计算某月的天数
    Unity3D导出的EXE不用显示分辨率选择界面
    Unity3D 之暂停和继续的实现
    double的值太大,以及补0
    Unity3D鼠标点击物体产生事件
    java POi excel 写入大批量数据
    Unity3D 判断鼠标是否按在UGUI上
    Unity3D 之UGUI 滚动条
  • 原文地址:https://www.cnblogs.com/hycms/p/9769769.html
Copyright © 2020-2023  润新知