Tuesday, January 8, 2013

Track user login information using Asp.net,c# and javascript

user Information

In this article, i would like to explain the process of tracking user information using asp.net,c# and javascript.In my previous article i already explained how to create a login page using asp.net,c# and sqlserver and how to create logout using asp.net.this time i would like to explain the tracking of user login and logout information


Lets start Here is the list we are going to track user information

  • IP Address 
  • Location 
  • User 
  • Computer/Server 
  • Date Time 
First start with IP Address :

How to get client IP..?

Solution :


    string ipaddress = GetIPAddress();

     public string GetIPAddress()
    {
        try
        {
            string ipAdd;
            HttpRequest currentRequest = HttpContext.Current.Request;
            ipAdd = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (ipAdd == null || ipAdd.ToLower() == "unknown")
                ipAdd = currentRequest.ServerVariables["REMOTE_ADDR"];

            return ipAdd;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Next,How to get Location of user.?

Solution :
    
That's it, We successfully tracked the IP address and Location.
Now,Get the Server Information. Call the following method to get the computer information
string Serverinfo = GetServerInfo();

public string GetServerInfo()
    {
        try
        {
            string Sinfo;
            HttpRequest currentRequest = HttpContext.Current.Request;
            Sinfo = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (Sinfo == null || Sinfo.ToLower() == "unknown")
            {
                Sinfo = currentRequest.ServerVariables["REMOTE_ADDR"];
                Sinfo += "/" + currentRequest.ServerVariables["LOGON_USER"];
            }
            string[] computerinfo = Sinfo.Split('/');

            return computerinfo[1];
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Main part of the functionality is over.
Now its time to get the username,date and time.
For getting username,just read user session value like this
string UserInfo = Session["UserName"].ToString();
For date and time Write the following line
string Datetime = Convert.ToString(DateTime.Now);
Now we have all the information of the user.
Now its time to store the data in database. 
For this ,First we need to create an table called Employeeinformation 

Following are the columns. 
TrackId int primaryKey 
IPAddress varchar(15) 
Location varchar(15) 
User varchar(20) 
Server varchar(20) 
DateandTime datetime Save the Table...!!! 

Next,Again come back to .cs page and write the following logic inorder to insert the user information before this, call the method from page load


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string ipaddress = GetIPAddress();
            string Location=GetLocation();
            string Serverinfo = GetServerInfo();
            string UserInfo = Session["UserName"].ToString();
            string Datetime = Convert.ToString(DateTime.Now);
            InsertInformation(ipaddress)
        }
    }
    Public void InsertInformation(ipaddress,Location,Serverinfo,UserInfo,Datetime)
    {
string ConString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;

        SqlConnection con = new SqlConnection(ConString);
        
        SqlCommand com=new SqlCommand("insert into Employeeinformation(IPAddress,Location,User,Server,Datetime) values('"+ipaddress+"','"+Location+"','"+Serverinfo+"','"+UserInfo+"','"+Datetime+"')",con);
        com.ExecuteNonQuery();
    }
Click on the image to enlarge
That's it,Finally we have completed the coding part. 

Note : For explanation purpose i have called these methods in page load..for real time scenario we need to call these methods in button click event of login page. Check this tutorial. and call these methods. Now bind this information in Gridview then output will be look like this..

Labels: , , , , ,