记录IP,时间和用户代理

时间:2017-12-11 10:36:54

标签: javascript jquery asp.net

我正在实施一种机制,我可以在页面加载时通过电子邮件获得每个用户 IP,用户代理和时间(HH:Min:Sec:Millisec)。我在网站访问(页面加载)上调用所有java脚本函数时遇到问题。 我不需要任何按钮来调用事件。我将简单地部署一些页面,并将包含此机制,以便我可以记录所有用户。



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static bool SignUp_Email(string ipaddress, string time)
    {
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("xxx@outlook.com");
            message.To.Add("xyz@outlook.com");
            message.IsBodyHtml = true;
            message.Subject = "user information";
            
            message.Body = Signup_BodyTEXTInternal(ipaddress, time);
            SmtpClient s = new SmtpClient();
            s.Send(message);

        }
        catch (Exception exp)
        {
            throw exp;
        }
        return true;
    }
    protected static string Signup_BodyTEXTInternal(string ipaddress, string time)
    {
        
       
        string MTBCMailBody = "";
        MTBCMailBody = "<table width='700px' cellpadding='4' cellspacing='0'><tr><td colspan='2' align='justify' style='color: #000000; font-size: 13; font-family:verdana; white-space:nowrap;'>A New Demo Request has been received.</td></tr> " +
                        "<tr><td align='justify' style='color: #000000; font-size: 11; font-family:verdana'><strong>ip address:</strong> </td><td align='justify' style='color: #000000; font-size: 11; font-family:verdana'>" + ipaddress.Trim() + "</td></tr>" +

                         "<tr><td align='justify' style='color: #000000; font-size: 11; font-family:verdana'><strong>User Name:</strong> </td><td align='justify' style='color: #000000; font-size: 11; font-family:verdana'>" + time.Trim() + "</td></tr>" +
                                              
                        "<tr><td align='justify' style='color: #000000; font-size: 11; font-family:verdana' colspan='2'>Regards,</td></tr>" +
                        "<tr><td align='justify' style='color: #000000; font-size: 11; font-family:verdana' colspan='2'>Test</td></tr>";
        return MTBCMailBody;
    }

    [WebMethod]
    public static string getip(string ipaddress, string time)
    {
        SignUp_Email(ipaddress,time);
        string val = ipaddress;
        return "Reached";
    }
}
&#13;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

  <!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">
    <title></title>

  </head>

  <body>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>
      function addZero(x, n) {
        while (x.toString().length < n) {
          x = "0" + x;
        }
        return x;
      }

      function myFunction() {
        var d = new Date();
        var x = document.getElementById("demo");
        var h = addZero(d.getHours(), 2);
        var m = addZero(d.getMinutes(), 2);
        var s = addZero(d.getSeconds(), 2);
        var ms = addZero(d.getMilliseconds(), 3);
        x.innerHTML = h + ":" + m + ":" + s + ":" + ms;
      }
    </script>


    <script src="js/jquery-2.1.3.js"></script>
    <script type="text/javascript">
      function getIpval() {
        //location.href = "http://www.google.com/";
        //debugger;
        var ipAddress = $("#list").text();
        var time = $("[id$='demo']").val();


        var v = "{'ipaddress':'" + ipAddress + "','Time':'" + time + "'}";


        $.ajax({
          type: "POST",
          url: "Default.aspx/getip",
          data: v,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: onSuccessCall(),

          error: function(er) {
            console.log(er.responseText);
          }
        });


      }

      function onSuccessCall() {
        location.href = "http://www.google.com/";
      }
      var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

      if (RTCPeerConnection)(function() {
        var rtc = new RTCPeerConnection({
          iceServers: []
        });
        if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed
          rtc.createDataChannel('', {
            reliable: false
          });
        };

        rtc.onicecandidate = function(evt) {
          // convert the candidate to SDP so we can run it through our general parser
          // see https://twitter.com/lancestout/status/525796175425720320 for details
          if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
        };
        rtc.createOffer(function(offerDesc) {
          grepSDP(offerDesc.sdp);
          rtc.setLocalDescription(offerDesc);
        }, function(e) {
          console.warn("offer failed", e);
        });


        var addrs = Object.create(null);
        addrs["0.0.0.0"] = false;

        function updateDisplay(newAddr) {
          if (newAddr in addrs) return;
          else addrs[newAddr] = true;
          var displayAddrs = Object.keys(addrs).filter(function(k) {
            return addrs[k];
          });
          document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";
          //getIpval();
        }

        function grepSDP(sdp) {
          var hosts = [];
          sdp.split('\r\n').forEach(function(line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
            if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
              var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
                addr = parts[4],
                type = parts[7];
              if (type === 'host') updateDisplay(addr);
            } else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
              var parts = line.split(' '),
                addr = parts[2];
              updateDisplay(addr);
            }
          });
        }
      })();
      else {
        document.getElementById('list').innerHTML = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
        document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
      }
    </script>
  </body>

  </html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

Use  HttpContext.Current.Request.UserHostAddress to get IP Address.

Use Request.UserAgent on PageLoad to get Browser Details and more details 
via this


    private string GetBrowserDetails()
    {
    System.Web.HttpBrowserCapabilities browser = Request.Browser;

    string browserInfo = "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 = " + Request.UserAgent + "\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";

    return browserInfo;
    }