0

What I'd like to do is to create different cookie values for different browsers in .NET environment(cache with the same functionality is also ok, but I couldn't figure out how to use cache for that).

To clarify, I want users to be able to open my web app in different browsers simultaneously(e.g. on both Chrome and Firefox at the same time) where different values of a cookie can be used on the different browsers(for instance, cookie name is "MyCookieKey", i'd like to store a value V1 for Chrome and store a different value V2 for Firefox). And the cookie should be valid for solely the clientside.

What I've tried till now(separately):

HttpContext.Current.Request.Cookies,

HttpContext.Current.Response.Cookies,

HttpCookieCollection.

When I've used HttpContext.Current.Request or HttpContext.Current.Response, my app didn't work(i couldn't figure out what exactly the problem).

When I've used HttpCookieCollection, different browsers shared the cookie.

Regards.

Dilara Albayrak
  • 314
  • 1
  • 3
  • 23
  • browsers should not 'share' the cookie. Do you mean that you sent the same cookie to both browsers? If so, how are you testing for browser type? –  May 18 '17 at 12:42
  • I've created a cookie named "Domain". In my app user can change to url and connect other similar web app. In that cookie i keep selected domain of the application. For instance i've 2 domains, website1.com and website2.com and user is able to change the domain. The application's default url is website1.com, while using the app, user is enabled to change domain to website2. If user change the domain in Chrome, until browser is closing(or user logs out) the app runs in new selected url(website2.com). However, it also affects app's domain for other browser(for instance Firefox). ++ – Dilara Albayrak May 18 '17 at 12:59
  • ++ After user selects website2.com in Chrome, in Firefox, the default url changes into website2.com or vice versa. What i want is if user change domain, let it be kept on the browser but it does not affect the other browser. Domain is just an example here, consider it as something needs to be kept for the session for clientside and separately for different browsers. About testing, i simply rum my program for different browsers simultaneously, by doing that i see that it does not work as i wish. – Dilara Albayrak May 18 '17 at 13:00
  • Are you choosing the url based on the cookie, or based on the user? It may very well be worth posting that part of the code (where you decide the routing) –  May 18 '17 at 13:05
  • I choose cookie. However, i do not claim that it is wise thing to do. What i want to learn is how to keep a value client side on the browser basis(i.e. different values for different browser type). Currently, i use cache but it affects server. I'd like to post my code but since i've tried for multiple things, it got messy. I do not necessarily need code answer, i need a guidance how to implement such thing, by using which tool/class. – Dilara Albayrak May 18 '17 at 13:17
  • Okay. If you want to do this via cookie, one way is to set the domain in the cookie. Then, make sure you check this value in every request, and return the correct webpage based of that. this should look a bit like 'if(cookie.app==1){send app1}else{send app2}' (pseudocode) –  May 18 '17 at 13:26
  • Actually i do not insist on using cookie, but it is the best option i could think. If there is other way to do that, keep a value based on browser and let it be valid for the client side, i'd like to learn. Thanks though, i'll try your suggestion. – Dilara Albayrak May 18 '17 at 13:29
  • I'd also like to ask that when i search the internet for that issue, i see that everyone claims that each browser has own cookies for instance [here](http://stackoverflow.com/a/2153958). If that is true why would i experience this? – Dilara Albayrak May 18 '17 at 13:33

1 Answers1

0

Web Apps in general do not keep track of separate machines accessing them, they just keep track of different browser Sessions. Each instance of a client connecting to your app will generate a new session, and the information about this session is generally managed via a special cookie named ASP.NET_SessionId. The configuration for tracking the session allows changing the cookie name or to use non-cookie tracking. This tracking of Sessions can be utilized in a few different ways to differentiate between the separate browsers.

One method would be for your application to keep track of the session count. This would be done via global.asax and adding to the different application and session events. There are a few answers in the SO thread titled How to count Sessions in ASP.Net.

protected void Application_Start() {
    Application["LiveSessionsCount"] = 0;
}

protected void Session_Start() {
    Application["LiveSessionsCount"] = ((int)Application["LiveSessionsCount"]) + 1;
}

protected void Session_End() {
    Application["LiveSessionsCount"] = ((int) Application["LiveSessionsCount"]) - 1;
}

And when it is time to set your cookie; you could access that Application object to retrieve the count, and use that to create a different cookie value.

int LiveSessionsCount = (int) Application["LiveSessionsCount"];
string MyCookieValue = "V" + LiveSessionCount.ToString();
Response.Cookies["MyCookieKey"] = MyCookieValue;

The problem with this would be that the application is keeping track of current sessions. This will increase and decrease as connections to your site start and end. What you could do would be to add a second Application variable that has similar methods in the App/Session starts to start at 0 and increment; however, do not add in the Session End decrement of that value. This would then be a total count of sessions within the app cycle. When the site is restarted this would return to 0.

There are naturally other methods you can use, one of the sites I work with logs all connections into a database and worked with that way. This is just to give you some ideas on what can be done. I would read the documented links to get a better understanding of the application and session objects; as well as the session cookie.

Community
  • 1
  • 1
Mad Myche
  • 1,075
  • 1
  • 7
  • 15