Last post Mar 01, 2011 08:18 AM by chrisjduncan
Feb 28, 2011 04:06 PM|chrisjduncan|LINK
Very new to ASP.NET and C# (somewhat knowledgable in PHP) I have an ASP.NET web application and need to manage access to pages based on a 'status' stored in a cookie. I would like to use the following code to create the cookie in a login method.
HttpCookie cookie = new HttpCookie("UserCookie");
cookie.Value = "status"; <-- this will be replaced with a variable once its working
cookie.Expires = DateTime.Now.AddHours(1);
My problem is that the only place I can put this code where it works is in the global.asax Session_Start method. It simply doesn't work anywhere else. I am checking the cookie creation in firefox options and see the ASP.NET_SessionId created but nothing
else (This is all running on localhost).
Is the session status somehow interfering with the cookie creation? Do I need to configure something in Web.config? It would appear that the only time I can create a cookie is on Session_Start. Does anyone have any tips, perhaps I am missing something simple?
Thanks in advance, Chris
Feb 28, 2011 04:44 PM|AceCorban|LINK
Once you have your cookie created, you set values not by a .Value property, but by setting a value in it's collection:
cookie["someValue"] = someValue;
cookie["anotherValue"] = anotherValue;
You can make up several "variables" within the cookie. For yours, it would probably be most appropriate to have something like this:
cookie["status"] = status;
Feb 28, 2011 05:36 PM|chrisjduncan|LINK
Ah, thats good to know as that may well have been my next stumbling block. I'm still damned if I can get this cookie actually created though. I figure it must be some kind of configuration issue but the answers I find don't seem to apply to this problem.
There must be a reason why it works in the global.asax file but nowhere else. I've tried everything I can think of, putting it in the page_load method, even a blank page with a button pointing to the code doesnt work!
Feb 28, 2011 06:47 PM|AceCorban|LINK
I'm not sure, but I think the SetCookie updates an existing cookie. If that is the case, then if the Request didn't have the cookie to begin with, an update won't do anything (I could be wrong on this).
I've always used Response.Cookies.Add(cookie)
Other than that, maybe it's browser settings?
Feb 28, 2011 08:23 PM|chrisjduncan|LINK
Thanks again. I tried that but its still not the problem. I have found another bizarre problem where I can't even create a button in an aspx file and bind it to an on_click in the cs file. When I run it I get an error suggesting that it can't see the click
A few things started going wrong about the time I converted this from a web site to a web application. Could this be the root of my problems? I just rebuilt the entire project as an application and included namespaces this time but then the masterfile started
Can someone give me a very brief run down of the differences between a web site and web application?
Perhaps I should just copy all the code into a fresh website as oppose to web app?
Mar 01, 2011 06:09 AM|KumarHarsh|LINK
Mar 01, 2011 08:18 AM|chrisjduncan|LINK
Yes, thanks. You have returned 3750 results, all of which will tell me how to create a cookie. I believe this is my fault for not giving this post a more accurate title.
Anyway, problem solved. I believe it was something in the web.config file as when I rebuilt the application from fresh, debugging as I went, it worked. I can only think that the conversion from website to application borked something.