当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net网站首页根据IP自动跳转指定页面的示例

asp.net网站首页根据IP自动跳转指定页面的示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

澳德斯科奇,垂钓论坛,母亲哀悼词

对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有城市站点的功能,如果没有设置相应的城市站点,默认就是根据用户访问的ip地址的所在城市自动设置。本文主要通过自定义扩展ihttpmodule接口,考虑到性能ip数据库主要采用qqwry纯真ip数据库,主要实现根据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>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网