2015-06-22 70 views
1

我需要在MVC 5.搜索谷歌Request.ServerVariables [「REMOTE_ADDR」]以獲取本地系統的IP地址和瀏覽器代理(火狐,chorme,即等),這是不在MVC5工作獲取IP地址,瀏覽器類型MVC 5

+0

'Request.Browser'是你需要檢查瀏覽器的信息是什麼。 'Request.UserHostAddress'用於IP地址。在這兩種情況下,您可能無法保證正確的信息,因爲代理不會爲您提供正確的客戶端機器和瀏覽器的IP地址用戶代理始終可以被操縱。讓我知道我的觀點是否能幫助你。 – ramiramilu

回答

0

要獲得客戶端的IP地址

var IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
if (string.IsNullOrEmpty(IPAddress)) 
{ 
    IPAddress = Request.ServerVariables["REMOTE_ADDR"]; 
} 

爲了讓客戶端用戶代理。

var userAgent = Request.UserAgent; 
1

爲了得到IP地址使用此代碼:

public static string GetIPAddress(HttpRequestBase request) 
    { 
     string ip; 
     try 
     { 
      ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
      if (!string.IsNullOrEmpty(ip)) 
      { 
       if (ip.IndexOf(",") > 0) 
       { 
        string[] ipRange = ip.Split(','); 
        int le = ipRange.Length - 1; 
        ip = ipRange[le]; 
       } 
      } 
      else 
      { 
       ip = request.UserHostAddress; 
      } 
     } 
     catch { ip = null; } 

     return ip; 
    } 

https://stackoverflow.com/a/7348761/4568359

======================= =========================================

併爲獲得瀏覽器信息:

System.Web.HttpBrowserCapabilities browser = Request.Browser; 
string brw_info = "Browser Capabilities\n" 
    + "Type = "     + browser.Type + "\n" 
    + "Name = "     + browser.Browser + "\n" 
    + "Version = "     + browser.Version + "\n" 
    + "Major Version = "   + browser.MajorVersion + "\n" 
    + "Minor Version = "   + browser.MinorVersion + "\n" 
    + "Platform = "    + browser.Platform + "\n" 
    + "Is Beta = "     + browser.Beta + "\n" 
    + "Is Crawler = "    + browser.Crawler + "\n" 
    + "Is AOL = "     + browser.AOL + "\n" 
    + "Is Win16 = "    + browser.Win16 + "\n" 
    + "Is Win32 = "    + browser.Win32 + "\n" 
    + "Supports Frames = "   + browser.Frames + "\n" 
    + "Supports Tables = "   + browser.Tables + "\n" 
    + "Supports Cookies = "  + browser.Cookies + "\n" 
    + "Supports VBScript = "  + browser.VBScript + "\n" 
    + "Supports JavaScript = "  + 
     browser.EcmaScriptVersion.ToString() + "\n" 
    + "Supports Java Applets = " + browser.JavaApplets + "\n" 
    + "Supports ActiveX Controls = " + browser.ActiveXControls 
      + "\n" 
    + "Supports JavaScript Version = " + 
     browser["JavaScriptVersion"] + "\n"; 

https://msdn.microsoft.com/en-us/library/3yekbd5b.aspx

+0

(不確定對_HTTP_X_FORWARDED_FOR_和_spoofing_有什麼想法。) – greybeard