简体   繁体   English

使用Global.asax文件重定向用户时发生错误::页面未正确重定向

[英]Error when Redirecting user using Global.asax file ::Page's not redirected properly

Issues that I am facing and my questions are as follows:- 我面临的问题和问题如下:

  1. I am running this code on localhost only, is there any free service or something where I can run this sample webite of mine online? 我仅在localhost上运行此代码,是否有免费服务或可以在线运行我的示例Webite的东西? Else the IP address the follwoing code returns is always 127.0.0.1. 其他代码返回的IP地址始终为127.0.0.1。 How do I test my code? 如何测试我的代码? Currently I gave static values for IP address. 目前,我为IP地址提供了静态值。

  2. HTTP_X_FORWARDED_FOR is always returning null value. HTTP_X_FORWARDED_FOR始终返回空值。 I have read that only HTTP_X_FORWARDED_FOR contains the real IP address of a user. 我读到只有HTTP_X_FORWARDED_FOR包含用户的真实IP地址。

  3. Does REMOTE_ADDR contain real IP address of a user or the IP in it can also be spoofed? REMOTE_ADDR是否包含用户的真实IP地址,或者其中的IP也可以被欺骗? I just want to know the country in which the user is sitting in so I can redirect the user based on the ISO codes. 我只想知道用户所在的国家/地区,因此我可以根据ISO代码重定向用户。

  4. One major issue that I am facing is that I am unable to see MasterPageFile property in this Global.asax file. 我面临的一个主要问题是无法在此Global.asax文件中看到MasterPageFile属性。

    What I want is something like this :- 我想要的是这样的:-

If the ISO code is "FR" then the master to be loaded for the site is FranceMaster.master Else if ISO Code is "GB" then BritainMaster.master file is to be used. 如果ISO代码为“ FR”,则要为该站点加载的母版为FranceMaster.master;否则,如果ISO代码为“ GB”,则将使用UK.Master.master文件。

In what part of this file do I set the master pages?? 我在该文件的哪一部分设置母版页?

So basically I wanna learn how to Redirect a user based on the ISO code AND set the master page too. 因此,基本上,我想学习如何基于ISO代码重定向用户并设置母版页。 I gave hard coded IP address something like 122.87.234.1 then it gave me this error that Couldnt redirect properly. 我给了硬编码的IP地址,例如122.87.234.1,然后它给了我这个错误,导致无法正确重定向。 I googled about it and found out that the response.redirect request was being called indefinately. 我在Google上搜索了一下,发现response.redirect请求被不确定地调用。 If the following way is not how u redirect a user, how else one does it?? 如果以下方法不是您如何重定向用户,那么还有其他方法吗?

I am sure this kind of stuff has been implemented way too many times. 我确信这种东西已经实施了太多次了。 Could someone PLEASE help me out with this one?? 有人可以帮我解决这个问题吗? I would be grateful. 我会很感激。 Thanks. 谢谢。

below is the code I have written in Global.asax file :- 以下是我在Global.asax文件中编写的代码:-

<%@ Application Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="WorldDomination.Net" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    void Application_BeginRequest(object sender, EventArgs e)
    {
       string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(ip))
        {
            string[] ipRange = ip.Split(',');
            int le = ipRange.Length - 1;
            string trueIP = ipRange[le];
            ip = trueIP;
        }
        else
        {
            ip = Request.ServerVariables["REMOTE_ADDR"];
        }

       // string userHostIpAddress = "203.1.2.3";
        string userHostIpAddress = ip;
        IPAddress ipAddress;
        if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
        {
            string country = ipAddress.Country(); // return value: UNITED STATES
            string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
        }

        if (ipAddress.Iso3166TwoLetterCode() != null)
        {
            if (ipAddress.Iso3166TwoLetterCode().ToLower() == "fr")
            {
                Response.Redirect("www.mysite.fr");
            }
            if (ipAddress.Iso3166TwoLetterCode().ToLower() == "ja")
            {
                Response.Redirect("www.mysite.com/BlockedAccess.aspx");
            }
        }

else
{
 Response.Redirect("www.mysite.com");
}


    }

</script>

EDIT:- 编辑:-

@Chris :: Thanks. @克里斯::谢谢。 I am a bit confused. 我有点困惑。 If the IP address can always be spoofed, how can I ever get the country of a user? 如果IP地址始终是伪造的,我如何获得用户所在的国家/地区? I mean how to make sure the user location that I am fetching is correct? 我的意思是如何确保我要提取的用户位置正确? Also, there are like 100s of pages on the actual application project that I have to work on. 另外,我必须处理的实际应用程序项目上有数百页的内容。 Presently I was just trying it out by making a sample application with just one page. 目前,我只是通过制作一个只有一页的示例应用程序来进行尝试。 Now, it would be quite a task to set that masterpagefile property in ALL the pages. 现在,在所有页面中设置masterpagefile属性将是一项艰巨的任务。 Is there no way to get it done in a single place?? 有没有办法在一个地方完成它? There are literally hundereds of pages in that web application. 该Web应用程序中的页面确实有些杂乱无章。 Please reply. 请回复。 Thanks. 谢谢。

For the redirect error use Server.Transfer instead of Response.Redirect . 对于重定向错误使用Server.Transfer代替Response.Redirect The latter will actually initiate a new request and cause the Application_BeginRequest code to fire again, launching your user into an infinite loop. 后者实际上将启动一个新的请求,并导致Application_BeginRequest码再次触发,启动你的用户进入一个无限循环。

Regarding your other questions: 关于您的其他问题:

  1. Regarding hosting, I would just do a search for " free asp.net hosting ", or if you're willing to pay a few bucks for better service find a web host . 关于托管,我只想做“的搜索免费asp.net托管 ”,或者如果你愿意为更好的服务几块钱找到一个虚拟主机

  2. HTTP_X_FORWARDED_FOR will populate when the request is forwarded via a proxy server. HTTP_X_FORWARDED_FOR当请求是通过代理服务器转发将填充。 Since you're testing locally and never pass through a proxy server this variable will never be populated. 由于您在本地进行测试,并且永远不会通过代理服务器,因此永远不会填充此变量。 I also noticed that your code is grabbing the last value from HTTP_X_FORWARDED_FOR instead of the first. 我还注意到,您的代码被抓住的最后一个值从HTTP_X_FORWARDED_FOR而不是第一个。 I believe that will give you the IP address of the last proxy the user passed through and not the actual client IP (maybe someone else can confirm this). 我相信会给你最后一个代理用户通过传递的IP地址,而不是实际的客户端IP(也许别人可以证实这一点)。

  3. The user's IP address can always be spoofed. 始终可以欺骗用户的IP地址。

  4. If you want to dynamically switch your masterpage you will need to do this on the Page_Init event of every page. 如果要动态切换母版页,则需要在每个页面的Page_Init事件中执行此操作。 The best solution would likely be to set a session variable with the appropriate masterpage in your global.asax and then assign that as your masterpage in the Page_Init . 最好的解决办法很可能会设置一个会话变量在您的global.asax适当的母版,然后分配,作为在你的母版Page_Init

Global.asax: Global.asax:

if (ipAddress.Iso3166TwoLetterCode().ToLower() == "fr")
{
    Session["MasterPage"] = "~/FrenchMaster.master";
    Server.Transfer("www.mysite.fr");

On each page's Page_Init : 在每个页面的Page_Init

this.MasterPageFile = Session["MasterPage"].ToString();

If you're going to go down this route I would suggest creating a custom page class that implements this and that all of your pages in turn inherit from so you don't actually have to copy/paste this code to each page. 如果您打算沿这条路线走,我建议创建一个自定义页面类来实现此目的,并且所有页面又继承自该页面类,因此您实际上不必将此代码复制/粘贴到每个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM