Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading cookie in c#

Tags:

c#

cookies

I want someone who visits my internationalized site to be able to change the language. I thought best way would be to store the language chosen in a cookie - so when the page loads, it can look up the preferred language in the cookie.

Writing the cookie is going perfect, however I keep running into errors trying to read it. As I'm new to C# im having trouble translating the errors visual studio throws at me.

I want the value of a cookie called 'UserSettings' in a string called lang. I'm trying like this:

string lang = Request.Cookies["UserSettings"].Value;

The error it throws is:

Object reference not set to an instance of an object

I'm thinking the problem has to be on the right side of the statement, since I'm initializing the string on the left side.

I also tried making an instance of the cookie by doing

HttpCookie cookie = Request.Cookies["UserSettings"].Value;

but visual studio doesnt like this at all.

What am I doing wrong? Do I have to make an instance of the Request object? Any help would be appreciated.

I'm following different tutorials on this topic, but they all result in this same error.

EDIT: I've noticed I had

HttpCookie cookie = Request.Cookies["UserSettings"].Value;

I changed it to:

HttpCookie cookie = Request.Cookies["UserSettings"]; 

but no luck, it still didn't work.

EDIT: this is how my cookie is made

 public void Application_BeginRequest()
    {
        myCookie = new HttpCookie("UserSettings");
        myCookie.Value = "nl";
        myCookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(myCookie);
        hc = new HomeController();
    }

My cookie is 100% there, im absolutly sure, i can see it in Firefox-web developer.

UPDATE: this is the exact code i'm calling now

        public string getLang()
    {
       // HttpCookie aCookie = Request.Cookies["UserSettings"];
       //  string lang = Server.HtmlEncode(aCookie.Value);
       //  if (lang != null)
       // {
       //      currentLanguage = lang;
       //  }
        return currentLanguage;
    }

this way my code compiles, if I uncomment my code then it doest (error @ first line of this method)

like image 285
DeadManWalking Avatar asked Jun 14 '12 08:06

DeadManWalking


People also ask

What is cookie in C?

Cookies are small units of information that follow all request processes and web pages as they travel between Web browsers and servers. The above definition implies that every web page opened on a website has an exchange of cookies between the server and the web pages.

What values are stored in cookies?

Cookies allow a web site to store information on a user's machine and later retrieve it. The pieces of information are stored as name-value pairs. For example, a web site might generate a unique ID number for each visitor and store the ID number on each user's machine using a cookie file.

What is cookies How do you create and read cookies?

Cookies are created to identify you when you visit a new website. The web server — which stores the website's data — sends a short stream of identifying info to your web browser. Browser cookies are identified and read by “name-value” pairs. These tell cookies where to be sent and what data to recall.


1 Answers

It sounds like the cookie is never being set. In which case you need to check for this:

HttpCookie aCookie = Request.Cookies["UserSettings"];
if(aCookie != null) {
     object userSettings = aCookie.Value;
} else {
     //Cookie not set.
}

To set a cookie:

HttpCookie cookie = new HttpCookie("UserSettings");

cookie["UserSettings"] = myUserSettingsObject;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);

Here is a good article: http://www.java2s.com/Code/ASP/Session-Cookie/CreateandretrieveCookiedataC.htm

like image 126
jaypeagi Avatar answered Sep 22 '22 01:09

jaypeagi