根据IP将用户重定向到子域

Uma*_*han 7 .net c# redirect response.redirect http-redirect

我试图根据他的IP地址位置将用户重定向到子域.我有一个页面加载观察器,它在每个请求上运行一个函数,它获取用户位置,当我尝试重定向到另一个域时,它给我"太多重定向"错误,我无法找到解决此问题的方法.

目前我的代码如下所示

string CountryName = "";
var Country = HttpContext.Current.Response.Cookies["Country"];
Country.Expires = DateTime.Now.AddDays(365);
var ip = HttpContext.Current.Request.UserHostAddress;

if (!string.IsNullOrEmpty(ip) && ip != null && ip != "127.0.0.1")
{
    using (var client = new WebServiceClient(xxxxx, "xxxxxxxx"))
    {
        var IpCountry = client.Country(ip);
        CountryName = IpCountry.Country.Name;
    }
    switch (CountryName)
    {
        case "Denmark":
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                HttpContext.Current.Response.Redirect("/");
            }
            break;
        case "United Kingdom":
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                HttpContext.Current.Response.Redirect("/en");
            }
            break;
        case "Germany":
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                HttpContext.Current.Response.Redirect("/de");
            }
            break;
        case "Sweden":
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                HttpContext.Current.Response.Redirect("/se");
            }
            break;
        case "Norway":
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                HttpContext.Current.Response.Redirect("/no");
            }
            break;
        default:
            if (Country.Value != CountryName)
            {
                Country.Value = CountryName;
                //HttpContext.Current.Response.Redirect("http://www.google.com");
            }
            break;
    }
}
else if (loadedArgs.pageview.Area.ID != 2)
{
    HttpContext.Current.Response.Redirect("/choose-country");
}
Run Code Online (Sandbox Code Playgroud)

此外,我还想知道可能有哪些其他可能的方法以更好的方式处理这种情况,因此一旦设置了cookie,这些代码就不会在每个页面加载上运行.提前谢谢了.

Ami*_*nul 0

您可以使用下面的代码来获取客户端IP地址。即请求您网站中的页面的机器的 IP 地址。

\n\n
String UserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];\nif (string.IsNullOrEmpty(UserIP))\n{\n    UserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是我们需要用于 API URL 的格式。

\n\n
freegeoip.net/{format}/{IP_or_hostname}\n
Run Code Online (Sandbox Code Playgroud)\n\n

format \xe2\x80\x93> 是响应输出格式。它可以是 CSV、XML 或 JSON。

\n\n

例如,如果我们想获取 JSON 格式的响应,那么我们可以使用 API URL 作为“ http://freegeoip.net/json/clientIPAddress_here

\n\n
string url = "http://freegeoip.net/json/" + UserIP.ToString();\nWebClient client = new WebClient();\nstring jsonstring = client.DownloadString(url);\n
Run Code Online (Sandbox Code Playgroud)\n\n

要获取示例响应格式,请直接在浏览器中运行此http://freegeoip.net/json/XX.XX.XX.XX(将 XX.XX.XX.XX 替换为所需的 IP 地址)URL。您将看到以下响应格式。

\n\n
{\n"ip":"xxx.xxx.xxx.xxx",\n"country_code":"",\n"country_name":"",\n"region_code":"",\n"region_name":"",\n"city":"",\n"zip_code":"",\n"time_zone":"",\n"latitude":0,\n"longitude":0,\n"metro_code":0\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用下面的代码将响应转换为 JSON 对象,然后读取 JSON 对象的值并保存到会话变量或其他变量。

\n\n
dynamic dynObj = JsonConvert.DeserializeObject(jsonstring);\nSystem.Web.HttpContext.Current.Session["LocId"] = dynObj.country_code;\n
Run Code Online (Sandbox Code Playgroud)\n\n

您还可以使用Location标头检测国家/地区\nHttpWebRequest您可以设置AllowAutoRedirect为 false 来自行处理重定向。

\n\n
// don\'t allow redirects, they are allowed by default so we\'re going to override\nmyRequest.AllowAutoRedirect = false;\n\n// send the request\nHttpWebResponse response = myRequest.GetResponse();\n\n// check the header for a Location value\nif( response.Headers["Location"] == null )\n{\n  // null means no redirect\n}\nelse\n{\n  // anything non null means we got a redirect\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

解决“重定向过多”问题: \n重定向循环就像 > “A 指向 B,B 又指向 A”的情况\n。这样的重定向将使浏览器处于无限循环中,并且网页将永远不会显示。在过去,这种重定向或无限循环常常导致浏览器挂起。值得庆幸的是,现代浏览器能够检测到这种重定向循环,并通过显示错误消息来打破循环:\xe2\x80\x9cError 310 (net:: ERR_TOO_MANY_REDIRECTS): 重定向过多\xe2\x80\x9d。

\n\n

这个问题可能是客户端引起的,也可能是服务器端引起的。如果服务器端没有重定向循环,那么只需从浏览器中删除cookie就可能解决问题。

\n\n

所以我建议请检查你的routeconfig.cs中是否有这样的路由或类似的重定向操作,在从浏览器中删除cookie后指向自身

\n\n

希望这可以帮助 :)

\n