C# .Net Nginx组合获取客户端真实IP

本文介绍了如何在C#中使用Nginx的配置来获取客户端的真实IP,包括proxy_set_header指令的应用,以及通过HttpContextAccessor从ASP.NETCore获取IP的示例。同时提到了Startup.cs中的相关服务注册和API接口实现。
摘要由CSDN通过智能技术生成

引用地址:C# .Net Nginx组合获取客户端真实IP-罗分明网络博客

nginx.conf配置文件部分信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

server {

    listen 80;

    server_name www.luofenming.com;

    location / {

        proxy_pass http://localhost:8711;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header Host $host;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection keep-alive;

        proxy_cache_bypass $http_upgrade;

    }

}

C# 获取IP帮助类代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

using Microsoft.AspNetCore.Http;

using System.Linq;

namespace lqwvje

{

    public static class GetHostIP

    {

        /// <summary>

        /// 获取客户Ip

        /// </summary>

        /// <param name="context"></param>

        /// <returns></returns>

        public static string GetClientUserIp(this HttpContext context)

        {

            var userHostAddress = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();

            if (string.IsNullOrEmpty(userHostAddress))

            {

                //这个是直接IP,如果经过反向代理IP会变成代理机的,如果代理机就是本机就会变成127.0.0.1

                userHostAddress = context.Connection.RemoteIpAddress.ToString();

            }

            //最后判断获取是否成功,并检查IP地址的格式

            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))

            {

                return userHostAddress;

            }

            return "127.0.0.1";

        }

        /// <summary>

        /// 检查IP地址格式

        /// </summary>

        /// <param name="ip"></param>

        /// <returns></returns>

        public static bool IsIP(string ip)

        {

            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

        }

    }

}

获取IP调用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

//Startup.cs 文件注册服务务

 public void ConfigureServices(IServiceCollection services)

 {

     services.AddControllers();

     services.AddSwaggerGen(c =>

     {

         c.SwaggerDoc("v1"new OpenApiInfo { Title = "lqwvje", Version = "v1" });

     });

     //注册下面服务

     services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

 }

  

  

//下面是接口返回IP的方法

[Route("api/[controller]")]

[ApiController]

public class ValuesController : ControllerBase

{

    [HttpGet("GetIP")]

    public string GetIP()

    {

        HttpContextAccessor context = new HttpContextAccessor();

        return GetHostIP.GetClientUserIp(context.HttpContext);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值