Get Unique Browser Id For All Tabs In ASP.NET

I have searched on the internet in many article about how one can get a unique browser id , but I wasn't finding what I was looking for. Here we can easily get a unique id for each browser.

When we create a session in ASP.NET web application, then these sessions merge into each other on each tab. This is the solution for how we can make each session unique and the browser dosen't have a unique id so we can get a unique id through some small steps.Where do we use this approach? In many cases we need some unique id to monitor or count how many browsers are open on your website currently.
 
Step 1

Use the below code in page load event for storing some unique value in browser cookie.
  1. string hostName = Dns.GetHostName(); // Retrive the Name of HOST  
  2.             string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();  
  3.             string _value = GetValue(myIP+"TestWeb");  
  4.             if(!string.IsNullOrEmpty(_value))  
  5.             {  
  6.                 Response.Write(_value);  
  7.             }  
  8.             else  
  9.             {  
  10.                 SetValue(myIP+"TestWeb", Session.SessionID);  
  11.                 Response.Write(GetValue(myIP+"TestWeb"));  
  12.             }  

Step 2

Manage cookie value -- we need the below functions for getting and adding values in cookies.

  1. //Add Value in cokies    
  2.        public void SetValue(string key, string value)    
  3.        {    
  4.            Response.Cookies[key].Value = value;    
  5.            Response.Cookies[key].Expires = DateTime.Now.AddDays(1); // ad    
  6.        }    
  7.     
  8.        //Get value from cokkie    
  9.        public string GetValue(string _str)    
  10.        {    
  11.            if (Request.Cookies[_str] != null)    
  12.                return Request.Cookies[_str].Value;    
  13.            else    
  14.                return "";    
  15.        } 

 

Ebook Download
View all
Learn
View all