2017-03-07 11 views
0

私の悪い英語を残念に残念。クライアントからWeb ApiへのIP情報を取得する

私はログインオプションでIPを取得してデータベースに「セッション」として保存し、誰がアプリケーションを使用しているのかを登録しようとしています。

私はこれを試していますが、それはうまくいきません。

var ip = new System.Net.WebClient().DownloadString("http://ipinfo.io/json"); 

IPクライアントを取得します。だから私はクライアント側でこれを行う必要があることは論理的です。しかし、問題は、ログインして私に虚偽のデータを送信するために、ユーザーがこの値を変更することができます

$http.get("http://ipinfo.io/json").then(function (response) { 
    return response.data; 
}).catch(function (response) { 
    console.log(response.data); 
}); 

のWeb APIに送信するその前に、クライアントは、この値を変更することができますし、私はどうかを検証する方法がないということですこの情報は有効または実数です。だから、質問は¿私はユーザーがこのデータを操作させないでこれを行うことができますか?

+0

代わりにあなたがhttps://ipinfo.io/developersごとにhttp://ipinfo.io/ /JSONを要求する必要があり、サーバー上http://ipinfo.io/jsonを要求する –

答えて

0

Web APIでメソッドを作成すると、必要なすべての情報をデータベースに直接保存できます。

public static string UserIp() 
     { 
      string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
      if (string.IsNullOrEmpty(ip)) 
      { 
       ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      } 

      try 
      { 
       string url1 = "http://geoip.nekudo.com/api/" + ip.ToString(); // passing IP address will return location information. 
       WebClient client = new WebClient(); // Intialize the webclient 
       string jsonstring = client.DownloadString(url1); 
       dynamic dynObj = JsonConvert.DeserializeObject(jsonstring); // De-serialize the JSON string 
       string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\Logs\\" + "Ip.txt"; 
       using (System.IO.StreamWriter writer = new StreamWriter(filePath, true)) 
       { 
// you can save the information to database instead of writing to a file 

        writer.WriteLine("UserIp:" + ip); 
        writer.WriteLine("Date:" + DateTime.Now); 
        writer.WriteLine("JsonString:" + jsonstring); 
        writer.WriteLine("Country name:" + dynObj.country.code); 

       } 

       return dynObj; 


      } 
      catch (Exception ex) 
      { 
       string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\Logs\\" + "I.txt"; 
       string url1 = "http://geoip.nekudo.com/api/" + ip.ToString(); 
       WebClient client = new WebClient(); // Intialize the webclient 
       string jsonstring = client.DownloadString(url1); 
       dynamic dynObj = JsonConvert.DeserializeObject(jsonstring); 
       // string a = dynObj.country.code; 
       using (System.IO.StreamWriter writer = new StreamWriter(filePath, true)) 
       { 
        writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + 
            ex.StackTrace + 
            "" + Environment.NewLine + "Date :" + DateTime.Now.ToString()); 

        writer.WriteLine("UserIp:" + ip); 
        writer.WriteLine("Dynamic obj:" + dynObj); 
       } 
       return null; 
      } 


     } 
+0

は、正常ですローカルテストで取得するstring ip = ":: 1"? –

+0

私はあなたに何を言っているのか教えてくれませんでしたか? – ISHIDA

+0

このHttpContext.Current.Request.ServerVariables ["HTTP_X_FORWARDED_FOR"];私を得る。 このHttpContext.Current.Request.ServerVariables ["REMOTE_ADDR"];私を ":: 1"を価値として得てください。 –

関連する問題