By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,189 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,189 IT Pros & Developers. It's quick & easy.

ASP.net cookies not passing data.

BezerkRogue
P: 68
I am trying to use cookies to manage session states in an ASP.NET application. The states need to persist only while the browser session is open.

My web.config setting is:
<system.web>
<sessionState cookieless="UseCookies"/>

I have tried using the following code to create the cookies:

Response.Cookies("FLI").Value = cmpFLI

and I have tried:

Dim UID As New HttpCookie("UID")
UID.Value = txtUserName.Text
Response.Cookies.Add(UID)

I am using the following code to read the cookies:

If (Request.Cookies("UID") IsNot Nothing) Then
strUser1 = Request.Cookies("UID").Value
End If

It is either not passing the data or not reading it. What am I missing here?
Jan 9 '08 #1
Share this Question
Share on Google+
10 Replies


Plater
Expert 5K+
P: 7,872
This bit:
<sessionState cookieless="UseCookies"/>

Is used for the Session object, not for the actual .Cookies collection.
From your description, you should be using the Session object anyway and not the Cookies

That aside, if you reall want cookies, I've had no trouble using a function like this:
Expand|Select|Wrap|Line Numbers
  1. protected void RecordCookie()
  2.     {
  3.         HttpCookie cookie = new HttpCookie("preferences1");
  4.         cookie.Values.Add("PhoneNumber", tbPhoneNumber.Text  );
  5.         cookie.Values.Add("EmailAddress", tbEmailAddress.Text   );
  6.         cookie.Values.Add("Company", tbCompany.Text  );
  7.         //cookie.Values.Add("OrderedBy", tbOrderedBy.Text);
  8.         //Set how long to keep the cookie
  9.         cookie.Expires = DateTime.MaxValue;
  10.         if (Request.Cookies["preferences1"] == null)
  11.         {   
  12.             Response.Cookies.Add(cookie);
  13.         }
  14.         else
  15.         {
  16.             Response.SetCookie(cookie);
  17.         }
  18.     }
  19.  
Jan 9 '08 #2

BezerkRogue
P: 68
I tried the session object but I have to maintain a variable throughout the site. Since I can't predict the path that the user will take, it is difficult to account for the session item throughout the site.

How does this function code look in vb?
Jan 9 '08 #3

Plater
Expert 5K+
P: 7,872
I tried the session object but I have to maintain a variable throughout the site. Since I can't predict the path that the user will take, it is difficult to account for the session item throughout the site.

How does this function code look in vb?
The Session object maintains a collection of objects for a particular end user through out their use of the site. Clossing the browser, or going idle for 20mins(default time) will empty the data stored in the session object for the given user.
The session object is available to all aspx pages.

If you want to hold objects for all users use a static class (module in vb?)



As for my cookie code, it was pretty much the same as yours
Jan 9 '08 #4

BezerkRogue
P: 68
I got the code in place but I don't seem to be getting any data passed. Here is what I have for the cookie creation:
Expand|Select|Wrap|Line Numbers
  1.  'Create cookies
  2.         Dim UID As HttpCookie = New HttpCookie("UID")
  3.         UID.Value = txtUserName.Text
  4.         UID.Expires = "1 / 10 / 2008"
  5.         Response.Cookies.Add(UID)
  6.  
  7.         Dim PWD As HttpCookie = New HttpCookie("PWD")
  8.         PWD.Value = txtPassword.Text
  9.         Response.Cookies.Add(PWD)
  10.  
  11.         Response.Redirect("Default.aspx")
  12.  
And this is what I have for reading the cookies:
Expand|Select|Wrap|Line Numbers
  1.  If (Request.Cookies("UID") IsNot Nothing) Then
  2.             ckUID = Request.Cookies("UID").Value
  3.         Else
  4.             Response.Redirect("Invalid.aspx")
  5.         End If
  6.  
  7.         If (Request.Cookies("PWD") IsNot Nothing) Then
  8.             ckPWD = Request.Cookies("PWD").Value
  9.         Else
  10.             Response.Redirect("Invalid.aspx")
  11.         End If
  12.  
Is there anything else that I might be missing?
Jan 10 '08 #5

Plater
Expert 5K+
P: 7,872
I don't think you can add cookies with a response.redirect()
Since redirect clears out the response and sends a special http header.
Jan 10 '08 #6

BezerkRogue
P: 68
ok. Would I use Server.Transfer in this case?
Jan 10 '08 #7

Plater
Expert 5K+
P: 7,872
I guess?
Why are you making a cookie for each value though?
You can store multiple name=value pairs in a single cookie.
Jan 10 '08 #8

BezerkRogue
P: 68
Depending on the function the user is trying to access, I may only need to pull one value. I'm not real saavy on how cookies work code wise so I gravitated to the path of least resistance.
Jan 10 '08 #9

Frinavale
Expert Mod 5K+
P: 9,731
Depending on the function the user is trying to access, I may only need to pull one value. I'm not real saavy on how cookies work code wise so I gravitated to the path of least resistance.
Why don't you just create a User Class:
Expand|Select|Wrap|Line Numbers
  1. Public Class MySiteUser
  2.      Private _userID
  3.      Private _pwd
  4.      Public Property UserID As String
  5.           Get
  6.                Return _userID
  7.           End Get
  8.           Set(ByVal value As String)
  9.                _userID=value
  10.           End Set
  11.      End Property
  12.      Public Property PWD As String
  13.           Get
  14.                Return _pwd
  15.           End Get
  16.           Set(ByVal value As String)
  17.                _pwd=value
  18.           End Set
  19.      End Property
  20.     Public Function ValidateUser As Boolean
  21.         'If the user is valid pass back true otherwise false
  22.     End Function
  23.     Public Sub New(ByVal name As String, ByVal pwd As String)
  24.         'lalala initialize 
  25.     End Sub
  26. End Class
  27.  
When the user visits your site, create an instance of your user class and store it in session. A Session Identifier Cookie is automatically set for the user to identify their browser with their session. Then you can check the user on any page in your website by checking the user class...

Expand|Select|Wrap|Line Numbers
  1. Private _theUser As MySiteUser
  2.  
  3.  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4.     _theUser = Session("_theUser")
  5.     If _theUser is Nothing OrElse _theUser.ValidateUser = False
  6.         Response.Redirect("Invalid.aspx")
  7.     End IF
  8. End Sub
  9.  
Of course you're going to have to create this variable and store it in session in your Login Page:
Expand|Select|Wrap|Line Numbers
  1. Private _theUser As MySiteUser
  2.  
  3.  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4.     _theUser = Session("_theUser")
  5.     If _theUser is Nothing OrElse _theUser.ValidateUser = False
  6.        _theUser = New MySiteUser(TXT_LoginName.Text, TXT_PWD.Text)
  7.        Session("_theUser") = _theUser
  8.     End IF
  9.     If _theUser.ValidateUser = True Then
  10.         Response.Redirect("WelcomeDefault.aspx")
  11.     Else
  12.         LBL_Error.Text="You have provided invalid crednetials"
  13.     End If
  14. End Sub
  15.  
Of course the examples I've provided by no means provide any security on passwords. You shouldn't be storing passwords anyways...especially not in cookies. You should research how password hashes work and how you can use them...

Also, by using sessions instead of cookies stores important user information on your server instead of the client's browser...where they are vulnerable to being stolen, modified... etc.

The User Class can be used to track anything you want....where they've been in the site...what they have already provided on another page....anything you want really.

Check out this quick overview of Sessions...I think it'll help you get a better understanding of how to use them.


-Frinny
Jan 10 '08 #10

BezerkRogue
P: 68
wow. I never thought about that at all. The main item that I need to look at from any page is the user id. The user is assigned permissions and once they login successfully, the security test is only looking for their proscribed permission levels.

I will look at password hashes as well.

Thanks.
Jan 11 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.