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.
- string hostName = Dns.GetHostName();
- string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
- string _value = GetValue(myIP+"TestWeb");
- if(!string.IsNullOrEmpty(_value))
- {
- Response.Write(_value);
- }
- else
- {
- SetValue(myIP+"TestWeb", Session.SessionID);
- Response.Write(GetValue(myIP+"TestWeb"));
- }
Step 2
Manage cookie value -- we need the below functions for getting and adding values in cookies.
-
- public void SetValue(string key, string value)
- {
- Response.Cookies[key].Value = value;
- Response.Cookies[key].Expires = DateTime.Now.AddDays(1);
- }
-
-
- public string GetValue(string _str)
- {
- if (Request.Cookies[_str] != null)
- return Request.Cookies[_str].Value;
- else
- return "";
- }