zoukankan      html  css  js  c++  java
  • 【转】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

  • 相关阅读:
    docker
    电商项目查询模块思路(ElasticSearch)
    Elasticsearch简单的使用步骤以及查询模块的实现
    string常用方法
    通用 Mapper常用方法
    Linux防火墙操作
    简单SQL语句
    InnoDB基础
    浅析索引
    python爬虫面试题集锦及答案
  • 原文地址:https://www.cnblogs.com/hycms/p/9769769.html
Copyright © 2011-2022 走看看