DeadManWalking
DeadManWalking

Reputation: 237

Reading/Writing Cookies

I joined this site today hoping someone would be kind enough to explain to me what i'm doing wrong with cookies in ASP.NET. I'm still learning so apoligies if my question is too basic, but i cannot find the answer on google. Every answer i find shows the code I already have.

I am experimenting with creating and reading cookies, i have put this code in my applications constructor. this is how i try to initialize my cookie and add it to the browser.

global.asax.cs

    public MyApplication()
    {
        myCookie = new HttpCookie("UserSettings");
        myCookie.Value = "nl";
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);
    }

a method in HomeController.cs (trying to read the cookie)

    public void setLang(string lang)
    {
        HttpCookie myCookie = Request.Cookies["UserSettings"];
        myCookie.Value = lang;
        //rest of method

I am getting an error at Response.Cookies.Add(myCookie); [HttpException (0x80004005): Response is not available in this context.]

My thought is that i might have forgot to import a namespace or something, but nothing i do seems to fix this error, can anyone point me in the right direction?

Upvotes: 9

Views: 32536

Answers (4)

jishan siddique
jishan siddique

Reputation: 1873

You can use predefined namespace in .Net. Like this:

System.Web.HttpContext.Current.Response.addCookie(cookieobject);

Upvotes: -1

jishan siddique
jishan siddique

Reputation: 1873

Cookie's common property:

1.Domain => Which is used to associate cookies to domain.

2.Secure  => We can enable secure cookie to set true(HTTPs).

3.Value    => We can manipulate individual cookie.

4.Values  => We can manipulate cookies with key/value pair.

5.Expires => Which is used to set expire date for the cookies. 

Advantages of Cookie:

1.Its clear text so user can able to read it.

2.We can store user preference information on the client machine.

3.Its easy way to maintain.

4.Fast accessing.

Disadvantages of Cookie

1.If user clear cookie information we can't get it back.

2.No security.

3.Each request will have cookie information with page. 

How to clear the cookie information?

1.we can clear cookie information from client machine on cookie folder

2.To set expires to cookie object userInfo.Expires = DateTime.Now.AddHours(1); It will clear the cookie with one hour duration

Upvotes: 0

jishan siddique
jishan siddique

Reputation: 1873

cookies is a small piece of information stored on the client machine. This file is located on client machines.Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines. We need to import namespace called Systen.Web.HttpCookie before we use cookie.

Type of Cookies? Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie

Non-Persist Cookie - A cookie has expired time Which is called as Non-Persist Cookie 

How to create a cookie?

Its really easy to create a cookie in the Asp.Net with help of Response object or HttpCookie

    HttpCookie userInfo = new HttpCookie("userInfo");
    userInfo["UserName"] = "Jishan siddique";
    userInfo["UserColor"] = "Black";
    userInfo.Expires.Add(new TimeSpan(0, 1, 0));
    Response.Cookies.Add(userInfo);

Upvotes: 0

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

You can't use the Global.asax constructor to add a cookie to the Response because the Global.asax constructor is called before the application starts processing the HTTP request.

Move your code from the Global.asax constructor to the Application_BeginRequest method:

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

The Global.asax has a number of different events that are fired, you just chose wrongly.

  • Application_Init: Fires when the application initializes for the first time.
  • Application_Start: Fires the first time an application starts.
  • Session_Start: Fires the first time when a user’s session is started.
  • Application_BeginRequest: Fires each time a new request comes in.
  • Application_EndRequest: Fires when the request ends.
  • Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.
  • Application_Error: Fires when an unhandled error occurs within the application.
  • Session_End: Fires whenever a single user Session ends or times out.
  • Application_End: Fires when the application ends or times out (Typically used for application cleanup logic).

(from http://en.wikipedia.org/wiki/Global.asax)

Upvotes: 21

Related Questions