Example of asp. net's homepage automatically jumping to a specified page based on IP

  • 2020-11-18 06:09:22
  • OfStack

For large and medium-sized websites, in order to enhance the user experience, often need to according to the users of different urban site push or show the corresponding personalized content, such as in some large portal news I can have the function of the city sites, if not set the corresponding city sites, the default is based on the user access IP address automatically set in the city. In this paper, IHttpModule interface is extended by custom. Considering the performance of IP database, QQwry pure IP database is mainly used to automatically redirect to the specified page according to the address or address segment of IP or the city where IP is located (Nginx is supported as the front-end reverse proxy server). The core code of this WebsiteSkip component is as follows:


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  Members of the 

        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)// Ignore local computer requests 
                //    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);  // The query 1 a IP address 

                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
            {

            }
        }
    }
}

In terms of deployment, it is very simple, mainly using the IHttpModule interface and adding the configuration of this component to the HttpModule node in ES16en.config. The access restriction or permission parameters can be set in ES19en_SystemWebsiteSkip.cfg.xml. Here is a simple configuration example:


<?xml version="1.0" encoding="utf-8" ?>
<NetOpen_System>
  <WebsiteSkip>
    <SkipedUrl>
    <add ipcity=" wenzhou " 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=" zhenjiang " 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>


Related articles: