Another Beginner question - return values

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.

Thanks again!!!

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?

Full User class below. I have a dozen properties I was trying to track on users when they come to site. Not a good candidate for a class?

there is a SaveCookie method for saving the cookie. I also load the User Object to the ViewState, and I save the values to a database.


[Serializable]
public class User
{
    //User Properties
    public int UserDataProvided { get; set; } //0 = Not found, 1= Full info, 2=Email only, 3=Refused
    public int TimesVisited { get; set; }
    public DateTime? LastVisitDateTime { get; set; }
    public int? UserType { get; set; }  //enum? Customer, Prospect, Nuture
    public int NbrHelpPagesVisited { get; set; }
    public string InfusionCustomerId { get; set; }
    public string Email { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string BusinessName { get; set; }
    public string ZipCode { get; set; }
    public string PrimaryPhoneNumber { get; set; }


    public User()
    {
        this.UserDataProvided = 0;
        this.TimesVisited = 0;
        this.NbrHelpPagesVisited = 0;
        
    }

    public void SaveCookie(User UserInput)
    {

        User CurrentUser = new User();
        CurrentUser = UserInput;

        CurrentUser.TimesVisited++;
        CurrentUser.NbrHelpPagesVisited++;
        CurrentUser.LastVisitDateTime = DateTime.Now;

        HttpContext.Current.Response.Cookies["UserCookie"]["UserDataProvided"] = CurrentUser.UserDataProvided.ToString();
        HttpContext.Current.Response.Cookies["UserCookie"]["TimesVisited"] = CurrentUser.TimesVisited.ToString();
        HttpContext.Current.Response.Cookies["UserCookie"]["NbrHelpPagesVisited"] = CurrentUser.NbrHelpPagesVisited.ToString();
        HttpContext.Current.Response.Cookies["UserCookie"]["LastVisitDateTime"] = CurrentUser.LastVisitDateTime.ToString();
        HttpContext.Current.Response.Cookies["UserCookie"]["UserType"] = CurrentUser.UserType.ToString();
        HttpContext.Current.Response.Cookies["UserCookie"]["InfusionCustomerId"] = CurrentUser.InfusionCustomerId;
        HttpContext.Current.Response.Cookies["UserCookie"]["Email"] = CurrentUser.Email;
        HttpContext.Current.Response.Cookies["UserCookie"]["LastName"] = CurrentUser.LastName;
        HttpContext.Current.Response.Cookies["UserCookie"]["FirstName"] = CurrentUser.FirstName;
        HttpContext.Current.Response.Cookies["UserCookie"]["BusinessName"] = CurrentUser.BusinessName;
        HttpContext.Current.Response.Cookies["UserCookie"]["ZipCode"] = CurrentUser.ZipCode;
        HttpContext.Current.Response.Cookies["UserCookie"]["PrimaryPhoneNumber"] = CurrentUser.PrimaryPhoneNumber;
        HttpContext.Current.Response.Cookies["UserCookie"].Expires = DateTime.Now.AddDays(999);
    }

    public User RequestCookie()
    {
        User CurrentUser = new User();

        if (CookieUtility.CookieExists("UserCookie"))  //check for cookie
        {
            //Set Current User with cookie data
            // ParseOrDefault methods excute a TryParse, and return a default value if the string cannot be parsed.
            CurrentUser.InfusionCustomerId = HttpContext.Current.Request.Cookies["UserCookie"]["infusionCustomId"];
            CurrentUser.NbrHelpPagesVisited = IntParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["nbrHelpPagesVisited"]);
            CurrentUser.UserType = IntParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["userType"]);
            CurrentUser.TimesVisited = IntParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["timesVisited"]);
            CurrentUser.UserDataProvided = IntParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["UserDataProvided"]);
            CurrentUser.LastVisitDateTime = DateTimeParseOrDefault(HttpContext.Current.Request.Cookies["UserCookie"]["lastVisitDateTime"]);
            CurrentUser.Email = HttpContext.Current.Response.Cookies["UserCookie"]["Email"];
            CurrentUser.LastName = HttpContext.Current.Response.Cookies["UserCookie"]["LastName"];
            CurrentUser.FirstName = HttpContext.Current.Response.Cookies["UserCookie"]["FirstName"];
            CurrentUser.BusinessName = HttpContext.Current.Response.Cookies["UserCookie"]["BusinessName"];
            CurrentUser.ZipCode = HttpContext.Current.Response.Cookies["UserCookie"]["ZipCode"];
            CurrentUser.PrimaryPhoneNumber = HttpContext.Current.Response.Cookies["UserCookie"]["PrimaryPhoneNumber"];
        }

And I figured out the original question;
From page_load -

CurrentUser = CurrentUser.RequestCookie();

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

^^^this.

Also, everything you do put into the cookie gets sent up and down the wire every request. Hello 1kb + http headers.

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.