473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Cookie problem in asp.net v1.1

I'm having an issue that hopefully someone here can help me out with. First a quick explanation:

I'm managing users of my site in a fairly custom way. (in other words I'm not using asp.net's built-in methods). I have my own database table for user info, and people can register on the site, and then log in. If they haven't logged in, they are considered a guest and I store their name as "guest" in a sessions table along with some other info (like their IP, referrer, browser info, etc.).

However, if they've logged in before and choose to "auto login" from then on - each time they visit the site, I want to log them in with info I leave in a cookie. Yes, I know this won't work if they have cookies disabled...I'm taking the risk.

So, here is my code for my Session_Start method in my Global.asax code file:

protected void Session_Start(Object sender, EventArgs e)
{
string SessionGUID = string.Empty;
if (Request.Cookies["CoolWebsite"] != null)
{
if (Request.Cookies["CoolWebsite"]["AutoLogin"] == "true")
{
// we'll make their cookie good for two weeks
Response.Cookies["CoolWebsite"].Expires = DateTime.Now.AddDays(14);
// now simulate logging them in by adding a session token
// for them to the database and also set it in their SessionGUID Session var
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(Request. Cookies["CoolWebsite"]["SystemUserName"].ToString(), SessionGUID);
Session.Add("SessionGUID", SessionGUID);
}
else
{
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(@"guest" , SessionGUID);
Session.Add("SessionGUID", SessionGUID);
}
}
else
{
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(@"guest" , SessionGUID);
Session["SessionGUID"] = SessionGUID;
}
}

And now, here is the code that executes once they've typed in their user name and password to log in (it's within a user control)
if (Page.IsPostBack == true) //they typed un/pw and clicked "OK"
{
if((SecurityManager.ValidateLogin(txtUserName.Text , txtPassword.Value) == true)
&& (SecurityManager.HasAdminPrivileges(txtUserName.Te xt) == true))
{
//update the cookie and change their "guest" session to
//their user name, then redirect to the Control Panel
Response.Cookies["CoolWebsite"].Expires = DateTime.Now.AddDays(14);
Response.Cookies["CoolWebsite"]["SystemUserName"] = txtUserName.Text;
Response.Cookies["CoolWebsite"]["AutoLogin"] = "true";
SecurityManager.UpdateUserSessionNameInDatabase(tx tUserName.Text, Session["SessionGUID"].ToString());
Response.Redirect("ControlPanel.aspx");
}
else
{
LoginFailedDiv.Attributes.Add("style", @"display:block;");
}
}
else
{
//not a postback, first time admin login page is being loaded.
//check for session token and see if user is an admin
if (Session["SessionGUID"] != null)
{
string UserName = SecurityManager.GetUserNameFromSessionToken(Sessio n["SessionGUID"].ToString());
if (SecurityManager.HasAdminPrivileges(UserName) == true)
{
Response.Redirect("ControlPanel.aspx");
}
}
}

Basically, if they try to hit the login page when they've already said they want to be logged in automatically, I just want to forward them to the ControlPanel.aspx page. Also - if they hit any page "behind" the Control Panel that requires them to have certain privileges, I don't want it taking them back to the login page when their session expires. I just want the cookie to get checked in Session_Start and allow them to keep doing what they were doing. But that's not happening. Every time the session expires or if it's a new session and they try to access a page behind the Control Panel it's taking them back to the login page before they can get back to the ControlPanel.aspx page.

Does anyone see something obvious that I'm missing here? I don't have the foggiest idea why the cookie information is not getting picked out during the Session_Start. The line of code where I'm testing to see if the "CoolWebsite" cookie is null or not keeps ending up going down to the "else" block because the cookie is null.

Thanks,
-Jason

Nov 24 '05 #1
1 1585
OK - I discovered something odd as I've been debugging. This problems only occurs after I've compiled a new build of my web application. Do cookies become invalid or something if you deploy updated versions of your web application assemblies?
".NET Developer" <a@b.com> wrote in message news:uz**************@TK2MSFTNGP12.phx.gbl...
I'm having an issue that hopefully someone here can help me out with. First a quick explanation:

I'm managing users of my site in a fairly custom way. (in other words I'm not using asp.net's built-in methods). I have my own database table for user info, and people can register on the site, and then log in. If they haven't logged in, they are considered a guest and I store their name as "guest" in a sessions table along with some other info (like their IP, referrer, browser info, etc.).

However, if they've logged in before and choose to "auto login" from then on - each time they visit the site, I want to log them in with info I leave in a cookie. Yes, I know this won't work if they have cookies disabled...I'm taking the risk.

So, here is my code for my Session_Start method in my Global.asax code file:

protected void Session_Start(Object sender, EventArgs e)
{
string SessionGUID = string.Empty;
if (Request.Cookies["CoolWebsite"] != null)
{
if (Request.Cookies["CoolWebsite"]["AutoLogin"] == "true")
{
// we'll make their cookie good for two weeks
Response.Cookies["CoolWebsite"].Expires = DateTime.Now.AddDays(14);
// now simulate logging them in by adding a session token
// for them to the database and also set it in their SessionGUID Session var
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(Request. Cookies["CoolWebsite"]["SystemUserName"].ToString(), SessionGUID);
Session.Add("SessionGUID", SessionGUID);
}
else
{
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(@"guest" , SessionGUID);
Session.Add("SessionGUID", SessionGUID);
}
}
else
{
SessionGUID = Guid.NewGuid().ToString("B");
SecurityManager.AddSessionTokenToDatabase(@"guest" , SessionGUID);
Session["SessionGUID"] = SessionGUID;
}
}

And now, here is the code that executes once they've typed in their user name and password to log in (it's within a user control)
if (Page.IsPostBack == true) //they typed un/pw and clicked "OK"
{
if((SecurityManager.ValidateLogin(txtUserName.Text , txtPassword.Value) == true)
&& (SecurityManager.HasAdminPrivileges(txtUserName.Te xt) == true))
{
//update the cookie and change their "guest" session to
//their user name, then redirect to the Control Panel
Response.Cookies["CoolWebsite"].Expires = DateTime.Now.AddDays(14);
Response.Cookies["CoolWebsite"]["SystemUserName"] = txtUserName.Text;
Response.Cookies["CoolWebsite"]["AutoLogin"] = "true";
SecurityManager.UpdateUserSessionNameInDatabase(tx tUserName.Text, Session["SessionGUID"].ToString());
Response.Redirect("ControlPanel.aspx");
}
else
{
LoginFailedDiv.Attributes.Add("style", @"display:block;");
}
}
else
{
//not a postback, first time admin login page is being loaded.
//check for session token and see if user is an admin
if (Session["SessionGUID"] != null)
{
string UserName = SecurityManager.GetUserNameFromSessionToken(Sessio n["SessionGUID"].ToString());
if (SecurityManager.HasAdminPrivileges(UserName) == true)
{
Response.Redirect("ControlPanel.aspx");
}
}
}

Basically, if they try to hit the login page when they've already said they want to be logged in automatically, I just want to forward them to the ControlPanel.aspx page. Also - if they hit any page "behind" the Control Panel that requires them to have certain privileges, I don't want it taking them back to the login page when their session expires. I just want the cookie to get checked in Session_Start and allow them to keep doing what they were doing. But that's not happening. Every time the session expires or if it's a new session and they try to access a page behind the Control Panel it's taking them back to the login page before they can get back to the ControlPanel.aspx page.

Does anyone see something obvious that I'm missing here? I don't have the foggiest idea why the cookie information is not getting picked out during the Session_Start. The line of code where I'm testing to see if the "CoolWebsite" cookie is null or not keeps ending up going down to the "else" block because the cookie is null.

Thanks,
-Jason

Nov 24 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
12
by: chrism | last post by:
Hello, I have a pop-up window that I would like to appear in front of the browser home page when a user opens IE. Problem is, I'd like it to never appear again if the user navigates back to the...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
2
by: kelly.pearson | last post by:
Is this a bug? I am trying to write a cookie that can be accessed by various .Net applications on our domain. However, whenever I add the domain property to the cookie, no errors get thrown but...
5
by: cbhoem | last post by:
Hi - I am trying my hand at python cookies. I'm confused about a few things though. Do the python cookies get written to a cookies text file? I have simple code below -- I see the cookie in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.