با سلام دوستان گرامی من یک کد جاوا اسکریپت از اینترنت گرفتم که ip اصلی یک کاربر را حتی اگر از پراکسی استفاده کنه به من میده . مشکل من اینجاست که نمی توانم آنرا در قسمت code behind داشته باشم و ذخیره کنم ، در اینترت روشهای مختلفی گفته شده مثلاً hiden filde ولی برای من کار نکرد کد بنده به صورت زیر است ممنون می شوم راهنمایی کنید :
کد جاوا اسکریپت که کاربر را نمایش می دهد
//get the IP addresses associated with an account function getIPs(callback) { var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; //bypass naive webrtc blocking using an iframe if (!RTCPeerConnection) { //NOTE: you need to have an iframe in the page right above the script tag // //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe> //<script>...getIPs called in here... // var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } //minimal requirements for data connection var mediaConstraints = { optional: [{ RtpDataChannels: true }] }; var servers = { iceServers: [{ urls: "stun:stun.services.mozilla.com" }] }; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate) { //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/ var ip_addr = ip_regex.exec(candidate)[1]; //remove duplicates if (ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } //listen for candidate events pc.onicecandidate = function (ice) { //skip non-candidate events if (ice.candidate) handleCandidate(ice.candidate.candidate); }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function (result) { //trigger the stun server request pc.setLocalDescription(result, function () { }, function () { }); }, function () { }); //wait for a while to let everything done setTimeout(function () { //read candidate info from local description var lines = pc.localDescription.sdp.split('\n'); lines.forEach(function (line) { if (line.indexOf('a=candidate:') === 0) handleCandidate(line); }); }, 1000); } //Test: Print the IP addresses into the console //getIPs(function (ip) { console.log(ip); });
-------------------------
getIPs(function (ip) {
var li = document.createElement("li");
li.textContent = ip;
از طریق کد بدست بیارید
با سلام خدمت جناب مدائنی عزیز کد زیر
Label1.Text = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
اگر کاربر از پروکسی استفاده کنه ip سرور پروکسی رو بر می گردونه ، لطفاً به سوال من دقت کنید . من ip واقعی کاربر رو می خواهم . با اون کد جاوا اسکریپت بدست می یاد ولی نمی تونم تو قسمت code behind از اون استفاده کنم ،
توسط Ajax به Code ارسال کنید
سلام آقای مدائنی چطوری اینکارو(توسط Ajax به Code ارسال کنید) انجام بدم ؟
کد جاوا اسکریپت من اینه :
//get the IP addresses associated with an account function getIPs(callback) { var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; //bypass naive webrtc blocking using an iframe if (!RTCPeerConnection) { //NOTE: you need to have an iframe in the page right above the script tag // //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe> //<script>...getIPs called in here... // var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } //minimal requirements for data connection var mediaConstraints = { optional: [{ RtpDataChannels: true }] }; var servers = { iceServers: [{ urls: "stun:stun.services.mozilla.com" }] }; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate) { //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/ var ip_addr = ip_regex.exec(candidate)[1]; //remove duplicates if (ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } //listen for candidate events pc.onicecandidate = function (ice) { //skip non-candidate events if (ice.candidate) handleCandidate(ice.candidate.candidate); }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function (result) { //trigger the stun server request pc.setLocalDescription(result, function () { }, function () { }); }, function () { }); //wait for a while to let everything done setTimeout(function () { //read candidate info from local description var lines = pc.localDescription.sdp.split('\n'); lines.forEach(function (line) { if (line.indexOf('a=candidate:') === 0) handleCandidate(line); }); }, 1000); }
و کدهای قسمت aspx ایناست :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upmypic.aspx.cs" Inherits="upmypic" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="GetIPAdress.js"></script> <link href="css/mycss.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <div style="text-align: center"> <script> getIPs(function (ip) { var li = document.createElement("li"); li.textContent = ip; document.getElementById("HiddenField1").value = ip; }); </script> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:HiddenField ID="HiddenField2" runat="server" /> <script> var now = new Date(); document.getElementById("HiddenField2").value = now; </script> <asp:Image ID="Image1" runat="server" /><br /><br /> <asp:LinkButton ID="LinkButton1" CssClass="mybtn" runat="server" OnClick="LinkButton1_Click">برای مشاهده عکس کلیک کنید</asp:LinkButton> </div> </form> </body> </html>
و کدهای code behind ایناست :
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class upmypic : System.Web.UI.Page { public string x; public string y; protected void Page_Load(object sender, EventArgs e) { x = HiddenField1.Value; y = HiddenField2.Value.ToString(); } }
حالا در رویداد page_Load متغیر x رو بجای اینکه ip به من بدهد مقدار null یا "" را بر می گرداند . در صورتی که اگر در code java script یک alert(ip) بگذاریم ip را می دهد . انگار که اول رویداد page load اتفاق می افتد بعد کدهای جاوا اسکریپت اجرا می شود .
حال اگر یک button روی فرم بگداریم و در رویداد button click متغیر x را با مقدار hiden fild پر کنیم ip را به درستی بر می گرداند . منها من می خواهم با رویداد page load این اتفاق بیفتد که شما گفته اید که (توسط Ajax به Code ارسال کنید) انجام دهم . ولی من نمی دانم چگونه خیلی ممنون می شوم راهنمایی کنید . یا یک تیکه کد مورد نظر را بنویسید . با تشکر فراوان .
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)