• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

Need to use a cookie with MVC3

I have an MVC3 application that uses JavaScript and jQury. I need to use a cookie to record certain values from the last user's session. So I have a few questions:

1) If the data is to be stored in the database, via the Model, is there any reason for a cookie?
2) Can I do 100% of the coe in JavaScript and just leave the data on the user's disk as a cookie with no involvement of the Model?

Please provide the code to do this in JavaScript, in the event I decide to do this client side and not involve the Model.

Thanks.
0
newbieweb
Asked:
newbieweb
  • 3
  • 2
1 Solution
 
ToddBeaulieuCommented:
You could do it either way. If you're using a membership provider that handles remembering who the user is (identifying them) then you probably wouldn't need to save the data on the client.

A recent project needed to remember the user w/o a provider in the mix, so we used a cookie.

There are tons of examples on writing a cookie from js so I won't bother posting the links. A quick search will get you there.

You can also write a cookie from the controller like this:

Response.Cookies["mycookie"].Value = "myvalue";
0
 
newbiewebSr. Software EngineerAuthor Commented:
Ok, thanks. It seems that creating a cookie in the controller saves the effort of coding methods in the Model and needing to create a new table, and possible sprocs, right?

What other code have you got handy for creating, writing to and reading from the cookie, using MVC?
0
 
ToddBeaulieuCommented:
Sure, if you're just trying to make things easier on the user (by remembering previous interactions) and it's not mission critical if it's deleted, then a cookie is an easy option.

You don't have to write it from the server, either. I guess it depends on the situation. Personally, I hate working in js so I'll do everything I can to code in c#.

So, reading the cookie is automatic. When the use hits your site, the browser automatically imports the cookie and passes the data from it along with the request.

Actually, this is better code. It shows fetching a cookie and also creating it. We're looking for who the user is. We store a few basic values in the cookie.
private void SetCookies(string firstname, string lastname, string email, string company)
{
    HttpCookie userCookie = new HttpCookie("userinfo");
    userCookie["firstname"] = firstname;
    userCookie["lastname"] = lastname;
    userCookie["email"] = email;
    userCookie["company"] = company;
    userCookie.Expires = DateTime.Now.AddYears(1);
    Response.Cookies.Add(userCookie);
}

protected Boolean RecognizeUser()
{
    bool userIsRecognized = true;
    if (Session["userinfo"] == null)
    {
        HttpCookie userCookie = Request.Cookies["userinfo"];
        if (userCookie == null)
        {
            userIsRecognized = false;
        }
        else
        {
            UserInfo newUser = new UserInfo()
            {
                FirstName = userCookie["firstname"] ?? String.Empty,
                LastName = userCookie["lastname"] ?? String.Empty,
                Email = userCookie["email"] ?? String.Empty,
                Company = userCookie["company"] ?? String.Empty,
            };
            Session["userinfo"] = newUser;
        }
    }
    return userIsRecognized;
}

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
 
newbiewebSr. Software EngineerAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now