代码之家  ›  专栏  ›  技术社区  ›  neda Derakhshesh

从IP地址获取国家信息的安全方法ASP.net MVC公司

  •  0
  • neda Derakhshesh  · 技术社区  · 7 年前

    我在多语言网站工作,我可以得到用户的IP地址,问题是,我正在寻找一个非常安全和可靠的方式来获得IP和给我的国家代码,我希望用户面对自己的语言网页,而进入我的网站,我用过一次,但它停止提供服务。有人知道怎么做吗?这是我的代码,我真的很感谢任何帮助。我正在寻找一个真正值得信赖的一个,谷歌或其他一些API,甚至任何代码的建议

        public async Task<ActionResult> Index()
        {
    
    
            try
            {
                string userIpAddress = this.Request.UserHostAddress;
                var client = new HttpClient
                {
                    BaseAddress = new Uri("https:// `I need API HERE` ")
                };
    
                var response = await client.GetAsync(userIpAddress);
    
                var content = await response.Content.ReadAsStringAsync();
    
                var result = (Response)new XmlSerializer(typeof(Response)).Deserialize(new StringReader(content));
                var country_name = result.CountryName;
                var country_code = result.CountryCode;
                TempData["Country_code"] = country_code;
                TempData["Country_name"] = country_name;
    
                if (country_code == "FR")
                {
                    return RedirectToAction("fr", "Home");
                }
                else if (country_code == "JP")
                {
                    return RedirectToAction("jp", "Home");
                }
                else if (country_code == "DE")
                {
                    return RedirectToAction("de", "Home");
                }
    
                else if (country_code == "NL")
                {
                    return RedirectToAction("nl", "Home");
                }
                else if (country_code == "CN")
                {
                    return RedirectToAction("cn", "Home");
                }
                else if (country_code == "DK")
                {
                    return RedirectToAction("dk", "Home");
                }
                else if (country_code == "RU")
                {
                    return RedirectToAction("ru", "Home");
                }
                else
                {
                    return RedirectToAction("en", "Home");
    
                }
    
    
            }
            catch
            {
                return RedirectToAction("en", "Home");
            }
    
    
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   John H    7 年前

    不要为此使用IP地址。它不适合这个问题。例如,如果一个英国用户带着他们的笔记本电脑去度假,并且使用来自不同国家的网站呢?他们可能仍然希望用英语查看网站,但你最终会用另一种语言向他们提供内容。

    相反,使用 Accept-Language

    当服务器无法通过另一种方式(如特定的URL)确定语言时,将使用此头提示,该方式由显式用户决策控制。

    一个例子

    StringWithQualityHeaderValue preferredLanguage = null;
    if (Request.Headers.AllKeys.Contains("Accept-Language"))
    {
        preferredLanguage = Request.Headers["Accept-Language"]
            .Split(',')
            .Select(StringWithQualityHeaderValue.Parse)
            .OrderByDescending(s => s.Quality.GetValueOrDefault(1))
            .FirstOrDefault();
    }
    
    if (preferredLanguage?.Value == "fr")
    {
        return RedirectToAction("fr", "home");
    }
    // Check for other languages.    
    // ...
    // If no redirects match, default to English.
    return RedirectToAction("en", "home");
    
    推荐文章