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

GeoLite2-City.mmdb下载

因为比较简单,直接上代码,代码展示获取ip地址的国家和城市信息

csharp 复制代码
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; }

    }
}

视图:

csharp 复制代码
@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>
}

效果:

相关推荐
white-persist2 分钟前
【vulhub spring CVE-2018-1270】CVE-2018-1270 Spring Messaging 远程命令执行漏洞 完整复现详细分析解释
java·服务器·网络·数据库·后端·python·spring
鬼先生_sir10 分钟前
MySQL进阶-事务与锁机制
数据库·mysql·mvcc
treacle田34 分钟前
达梦数据库-达梦数据库中link链接访问远程Sql Sever-记录总结
数据库·达梦-sqlserver
ClouGence36 分钟前
不用搭复杂系统,也能做跨地域数据迁移?
大数据·数据库·saas
xcjbqd039 分钟前
SQL中视图能否嵌套存储过程_实现复杂自动化报表逻辑
jvm·数据库·python
哇蛙蛙40 分钟前
H3CNE--23.ACL
服务器·网络·经验分享·网络协议·tcp/ip·h3cne
听*雨声1 小时前
软件设计师上午题5:数据库
数据库
hong78171 小时前
阿里coding plan qwen3.6-plus 不支持图片上下文长度只有200K,问题出在哪?
linux·运维·数据库
Paxon Zhang1 小时前
MySQL 大师之路**数据库约束,表设计,CRUD**
android·数据库·mysql
南無忘码至尊2 小时前
Unity学习90天-第2天-认识键盘 / 鼠标输入(PC)并实现WASD 移动,鼠标控制物体转向
学习·unity·c#·游戏开发