一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!

对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有城市站点的功能,如果没有设置相应的城市站点,默认就是根据用户访问的IP地址的所在城市自动设置。本文主要通过自定义扩展IHttpModule接口,考虑到性能IP数据库主要采用QQwry纯真IP数据库(但此数据库并非是官方的,我之前与ip138网站对比过,IP地址信息的准确性大概在90%左右),主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能(支持Nginx作为前端反向代理服务器),该WebsiteSkip组件核心代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using NetOpen_System.Component.QQWry;
 
namespace NetOpen_System.Component
{
    public sealed class WebsiteSkipHttpModule : IHttpModule
    {
        #region IHttpModule 成员
 
        public void Dispose()
        {
        }
 
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
 
 
        #endregion
 
 
        void context_BeginRequest(object sender, EventArgs e)
        {
            try
            {
 
                //if (HttpContext.Current.Request.IsLocal)//忽略本地计算机请求
                //    return;
 
                //string ip = HttpContext.Current.Request.UserHostAddress;
                //string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                string ip = string.Empty;
                if (HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
               
 
                QQWryLocator qqWry = new QQWryLocator(HttpContext.Current.Server.MapPath(@"~\IpData\qqwry.dat"));
 
                IPLocation ipaddress = qqWry.Query(ip);  //查询一个IP地址
 
                string ls_city = ipaddress.Country;
                string ls_urlfrom = string.Empty;
                string ls_urlto = string.Empty;
                string ls_url = HttpContext.Current.Request.Url.AbsoluteUri;
                string ls_useragentkeyword = string.Empty;
              
                ExcludeUserAgentMatchEngine Em = WebsiteSkipConfiguration.GetConfig().ExcludeUserAgents;
 
                if(Em.ExcludeUserAgentList.Count > 0)
                {
                    foreach (ExcludeUserAgent ua in Em.ExcludeUserAgentList)
                    {
                        if (HttpContext.Current.Request.UserAgent.Contains(ua.keyword))
                        {
                            return;
                        }
                    }
                }
 
                UrlMatchEngine pu = WebsiteSkipConfiguration.GetConfig().SkipedUrls;
 
                if (pu.UrlList.Count > 0)
                {
                    foreach (SkipedUrl sk in pu.UrlList)
                    {
              
                        if (ls_city.Contains(sk.IpCity))
                        {
                            if (sk.UrlFrom.Length > 0)
                            {
                                if (sk.UrlFrom.Contains(ls_url) && !ls_url.Contains(sk.OutKeyWord))
                                {
                                    if (sk.UrlTo.Length > 0)
                                    {
                                        HttpContext.Current.Response.Redirect(sk.UrlTo, true);
                                    }
                                    break;
                                }
                                
                            }
 
                            break;
                        }
                    }
                }
 
                if (WebsiteSkipConfiguration.GetConfig().IpChecks.GetIpIn(ip))
                {
                    ls_urlfrom = WebsiteSkipConfiguration.GetConfig().IpChecks.UrlFrom.Trim();
                    ls_urlto = WebsiteSkipConfiguration.GetConfig().IpChecks.UrlTo.Trim();
                    if (ls_urlfrom.Length > 0)
                    {
 
                        if (ls_urlfrom.Contains(ls_url) && !ls_url.Contains(WebsiteSkipConfiguration.GetConfig().IpChecks.OutKeyWord))
                        {
                            if (ls_urlto.Length > 0)
                            {
                                HttpContext.Current.Response.Redirect(ls_urlto, true);
                            }
              
                        }
 
                    
                    }
                }
              
                 
            }
            catch
            {
 
            }
        }
    }
}

在部署方面,非常简单主要利用IHttpModule接口并在Web.config中的HttpModule节点添加此组件的配置,访问限制或允许参数可以在NetOpen_SystemWebsiteSkip.cfg.xml进行设置,以下为一个简单的配置示例:

<?xml version="1.0" encoding="utf-8" ?>
<NetOpen_System>
<WebsiteSkip>
<SkipedUrl>
<add ipcity="温州" urlfrom="http://examplesite.com/Default.aspx,http://examplesite.com/,http://examplesite.cn/,http://www.examplesite.cn" urlto="http://wz.mainwebsite.pcom/index.aspx" outkeyword="math"/>
<add ipcity="镇江" urlfrom="http://examplesite.com/Default.aspx,http://examplesite.com/,http://examplesite.cn/,http://www.examplesite.cn" urlto="http://jszj.mainwebsite.com/index.aspx" outkeyword="math"/>
</SkipedUrl>
<SkipedIP>
<add ip1="220.186.0.0" ip2="220.186.255.255" urlfrom="http://examplesite.com/Default.aspx,http://examplesite.com/,http://examplesite.cn/,http://www.examplesite.cn" urlto="http://wz.mainwebsite.com/index.aspx" outkeyword="math"/>
</SkipedIP>
<ExcludeUserAgent>
<add keyword="Baiduspider">
<add keyword="Sosospider">
<add keyword="Sogou web spider">
<add keyword="Sogou inst spider">
<add keyword="Sogou-Test-Spider">
<add keyword="Sogou Orion spider">
<add keyword="Gigabot">
<add keyword="0JJJSpider">
<add keyword="Sogou Pic Spider">
<add keyword="Googlebot">
<add keyword="Yeti/1.0">
</ExcludeUserAgent>
</WebsiteSkip>
</WebsiteSkip>
</NetOpen_System>

该组件源代码下载地址:https://websiteskip.codeplex.com/,欢迎访问下载!虽然该组件实现并不复杂,原理也很简单,但较为实用,后续将增加根据IP138的网站进行实时查询,这样IP地址信息将更为精确,但对性能可能会有一些影响。

本博客为软件人生原创,欢迎转载,转载请标明出处:http://www.cnblogs.com/nbpowerboy/p/3164287.html 。演绎或用于商业目的,但是必须保留本文的署名软件人生(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。SharePoint商业智能技术QQ群:140668362,.Net技术交流QQ群:195516928,欢迎各位加入交流

 
 
分类: ASP.NETSharepoint

一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!的更多相关文章

  1. 一种基于自定义代码的asp.net网站访问IP过滤方法!

    对于一些企业内部核心系统,特别是外网访问的时候,为了信息安全,可能需要对外部访问的IP地址作限制,虽然IIS中也提供了根据IP地址或IP地址段进行限制或允许,但并没有提供根据IP地址所在的城市进行限制 ...

  2. asp.net - GridView根据linkButton值不同跳转不同页面

    一,当前页面中,前台界面的主要代码: <asp:TemplateField HeaderText="姓名"> <ItemTemplate> <!--根 ...

  3. asp.net中GridView传多个值到其它页面的方法

    网站开发中,在页面之间的跳转,经常会用到传值,其中可能会传递多个值. 一.CommadArgument传多个值到其他页面. 像Gridview dataList repeater等数据绑定控件中,可以 ...

  4. ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法

    mvc使用Area分区开发后,存在不同Area之间的跳转,需要为每个区间添加Area规则,如下: 按 Ctrl+C 复制代码 using System.Web.Mvc; namespace web.A ...

  5. asp.net 重写OnException返回json或跳转新页面

    protected override void OnException(ExceptionContext filterContext) { // 此处进行异常记录,可以记录到数据库或文本,也可以使用其 ...

  6. ASP.NET网站中设置404自定义错误页面

    在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...

  7. 新西兰程序员 ASP.NET网站中设置404自定义错误页面

    新西兰程序员 ASP.NET网站中设置404自定义错误页面 在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页 ...

  8. asp.net网站优化经验之谈

    最近公司一个项目上线后,经常崩溃,于是开始重视性能问题,在网上看到了许多相关文章,整理下发了上来.以备参考. 在用ASP.NET开发网站的时候,性能是永远需要考虑和关注的问题,性能不仅仅只是程序代码执 ...

  9. asp.net网站作为websocket服务端的应用该如何写

    最近被websocket的一个问题困扰了很久,有一个需求是在web网站中搭建websocket服务.客户端通过网页与服务器建立连接,然后服务器根据ip给客户端网页发送信息. 其实,这个需求并不难,只是 ...

随机推荐

  1. C++ ofstream和ifstream具体的方法和C语言file说明

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,事实上所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,全部的I/O都以这个"流"类为基础的,包含我 ...

  2. ASP.NET 5中的Sake与KoreBuild

    初识ASP.NET 5中的Sake与KoreBuild   从github上签出基于ASP.NET 5的MVC 6的源代码进行编译,发现有2个编译命令: build.cmd是针对Windows的编译命 ...

  3. 一个简单的dom查询函数

    var regid = /^#([\w-]*)$/, regClass = /^\.([\w-]*)$/, regName = /^(div|a|p|ul|li|input|select|docume ...

  4. Python 静态变量 与 静态方法

    静态变量: XXXClass.py: class XXXClass: CONST_Value = 10.1 Tester.py: import XXXClass print XXXClass.XXXC ...

  5. 小公司免费的ERP软件

    http://www.2bizbox.cn/ https://www.odoo.com/

  6. CSS学习笔记之CSS的继承、层叠和特殊性

    继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这个颜色设置不仅应用p标签 ...

  7. Unity3D第三人称摄像机控制脚本

    好久没有敲Blog该.感谢您的留言.注意.私人信件和其他支持,但我似乎没有办法继续自己曾经写了一篇博客系列,因为我在网上找到有关unity3D太少的内容,U3D相关的文章!.. 第三人称视角 第三人称 ...

  8. 2014年3I工作室成员的正式名单

    后3I认真审议和审查工作室的老师及相关人员,今天,新成员首次正式发布,如以下:博才文(11软件).黄彩云(11软件).朱小丹(11软件).海(11软件).欧剑灵(11此计).黄思源(12软件).黄龙营 ...

  9. Oracle入门4-REF Cursor

    Oracle入门4-REF Cursor 转自:http://blog.sina.com.cn/s/blog_55dbebb00100gxsc.html 自:http://blog.csdn.net/ ...

  10. "手机信号放大器" 让手机信号增强的办法

    我的住处信号及其的差,电话基本打不进来,放大器的话便宜的也得一百多,于是自己尝试各种办法让自己的手机有信号,于是奇迹般的成功了: 将手机放于铁缸内,并45度角斜放,刚好淹没手机为佳.于是信号奇迹般的3 ...