Another very basic problem. I am calling a method that I want to return an object, but the object isnt being updated when it returns.
Here is my stuff:
page_load method:
User CurrentUser = new User();
CurrentUser.RequestCookie();
if (CurrentUser.UserDataProvided == 0)
{
//Do code
}
User class:
[Serializable]
public class User
{
//User Properties
public int UserDataProvided { get; set; }
public User()
{
this.UserDataProvided = 0;
}
public User RequestCookie()
{
User CurrentUser = new User();
if (CookieUtility.CookieExists("UserCookie")) //check for cookie
{
//Set Current User with cookie data
CurrentUser.UserDataProvided = IntParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["UserDataProvided"]);
(note - never could figure out that extension method - but I figured I had learned enough over the weekend and I was just trying to get this to work)
}
return CurrentUser;
}
While debugging, I can see that the CurrentUser.UserDataProvided has the value that is retrieved from the Cookie. But when it returns to the Page_Load, it doesnt return the CurrentUser object. What am I leaving out?
And if you see anything else that is BAD, feel free to offer constructive criticism.
First of all, I don’t see the reason for the User class.
I think you can achieve what you want like this (if I understood correctly)
In Page_Load
var cookie=Request.Cookies["UserCookie"];
int userData=0; //or any default value you want
if (cookie!=null)
{
userData=IntParseOrDefault(ck.Values["UserDataProvided"]);
if (userData == 0)
{
//Do code
}
}
Btw, how do you set the cookie in the first place?
I don’t have time right now but after a quick look there is one thing that’s very important to know:
DO NOT STORE sensitive data (fullname, address, phone numbers) in a plain cookie!!!
Track the user only by id. If you want to store some other information, make sure is nothing that can be used against your users by a hacker. If you HAVE To store a sensitive data in a cookie (99.99% times you don’t) use SSL and an encrypted cookie.
if you don’t have SSL an encrypted cookie is the least you can do
You may want to look at ASP.NET Profile. ASP.NET comes with a baked solution which lets you specify the profile properties in web.config either explicitly or through a class you design. It avoids the potential security and scalability problems mentioned above and it has a solution for “migrating” profiles when (not if) you have to extend it or need to deprecate some properties.