• C#使用MaxMind.GeoIP2数据库查询当前ip地址


    GeoLite2-City.mmdb下载
    因为比较简单,直接上代码,代码展示获取ip地址的国家和城市信息

    using MaxMind.GeoIP2;
    using MaxMind.GeoIP2.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    
    namespace IP.Demo.API.Controllers
    {
        public class HomeController : Controller  C#有偿Q群:927860652
        {
            List<string> ipList = new List<string>();
            public ActionResult Index()
            {
    
                InitIplist();
                var ips =  GetIPInfos();
                return View(ips);
            }
    
            private void InitIplist()
            {
                // 获取客户端请求的IP地址
                var ipAddress = Request.UserHostAddress;
                //湖南
                var hn = "175.6.68.41";
                //台湾
                var tw = "210.61.221.0";
                //香港
                var hk = "58.176.0.0";
                //伦敦
                var ld = "165.3.120.76";
                ipList.Add(hn);
                ipList.Add(ipAddress);
                ipList.Add(tw);
                ipList.Add(hk);
                ipList.Add(ld);
            }
    
            private List<IPInfo> GetIPInfos() 
            {
                List < IPInfo > ips = new List<IPInfo>();
                var reader = new DatabaseReader("D:\\C#\\IP.Demo.API\\IP.Demo.API\\bin\\GeoLite2-City.mmdb");
                foreach (var ipAddress in ipList)
                {
                    try
                    {
                        // 解析IP地址
                        var ipAddressObj = IPAddress.Parse(ipAddress);
                        var response = reader.City(ipAddressObj);
                        IPInfo iPInfo = new IPInfo();
                        iPInfo.ip = ipAddress;
                        iPInfo.Country = response.Country.Names["zh-CN"];
                        try
                        {
                            iPInfo.City = response.City.Names["zh-CN"];
                        }
                        catch
                        {
                            iPInfo.City = response.MostSpecificSubdivision.Names["zh-CN"];
                        }
                        if (iPInfo.Country.Equals("中国"))
                        {
                            if (iPInfo.City.Equals("台湾") || iPInfo.City.Equals("香港")) iPInfo.IsDomestic = false;
                            else iPInfo.IsDomestic = true;
                        }
                        else
                            iPInfo.IsDomestic = false;
    
                        if (iPInfo.Country.Equals("中华民国")) { iPInfo.Country = "中国"; iPInfo.City ="台湾"; }
                        if (iPInfo.Country.Equals("香港")) { iPInfo.Country = "中国"; iPInfo.City = "香港"; }
    
                        ips.Add(iPInfo);
                    }
                    catch 
                    {
                        //处理本地127.0.0.1的请求
                        IPInfo iPInfo = new IPInfo();
                        iPInfo.ip = ipAddress;
                        iPInfo.Country = "未知";
                        iPInfo.City = "未知";
                        iPInfo.IsDomestic = false;
                        ips.Add(iPInfo);
                    }
                       
                }
                reader.Dispose();
                return ips;
            }
    
    
    
    
        }
    
        public class IPInfo {
            public string ip { get; set; }
    
            public string Country { get; set; }
    
            public string City { get; set; }
    
            public bool IsDomestic { get; set; }
    
        }
    }
    
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    视图:

    @using IP.Demo.API.Controllers;
    @model List<IPInfo>
    
    @foreach (var IPInfo in Model)
    {
        <div>
            <p>IP: @IPInfo.ip</p>
            <p>国家: @IPInfo.Country</p>
            <p>城市: @IPInfo.City</p>
            @if (IPInfo.IsDomestic)
            {
             <p>国内:</p>
            }
            else 
            {
             <p>国内:</p>
            }
    
        </div>
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    效果:
    在这里插入图片描述

  • 相关阅读:
    在Linux系统安装Kafka
    Ubuntu 22.04 远程桌面
    redis学习总结-可删除
    shell脚本循环语句
    【half done】剑指offer53:在排序数组中查找数字
    vite3+Ts+Prettier+ESLint
    C语言-深度剖析数据在内存中的存储
    MySQL数据库基础:JSON函数各类操作一文详解
    读论文-SOD-U2Net算法研究
    序列模型 - 机器翻译
  • 原文地址:https://blog.csdn.net/csdn2990/article/details/134534177