Read / Write Cookies

I joined this site today, hoping that someone would be kind enough to explain to me what I'm doing wrong with cookies in ASP.NET. I still study such apolig if my question is too simple, but I can not find the answer on google. Every answer I find shows a code that I already have.

I am experimenting with creating and reading cookies, I put this code in my application builder. 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); } 

method in HomeController.cs (attempt to read a cookie)

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

I get the error Response.Cookies.Add (myCookie); [HttpException (0x80004005): The response is not available in this context.]

My thought is that I may have forgotten to import the namespace or something like that, but nothing seems to fix this error, can someone point me in the right direction?

+6
source share
4 answers

You cannot use the Global.asax constructor to add cookies to Response, because the Global.asax constructor is called before the application starts processing the HTTP request.

Transfer 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); } 

In Global.asax there are several different events that were triggered, you simply mistakenly indicated.

  • Application_Init : runs when the application is initialized for the first time.
  • Application_Start : starts when the application is first launched.
  • Session_Start : Session_Start first time a user session starts.
  • Application_BeginRequest : triggered every time a new request arrives.
  • Application_EndRequest : fires when the request completes.
  • Application_AuthenticateRequest : indicates that the request is ready for authentication.
  • Application_Error : fires when an unhandled error appears in the application.
  • Session_End : triggered whenever a single user session ends or time runs out.
  • Application_End : triggered when an application ends or time runs out (usually used for application cleanup logic).

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

+15
source

A cookie is a small piece of information stored on a client machine. This file is located on client machines. It is used to store information about user preferences, such as Username, Password, City and PhoneNo, etc. On client computers. We need to import the Systen.Web.HttpCookie namespace before we use cookies.

Type of cookie? Persist Cookie - A cookie does not have an expired time called a Persist Cookie

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

How to create a cookie?

Its very easy to create a cookie in Asp.Net using the 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); 
0
source

Common cookie 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. 

Cookie Benefits:

 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. 

Cookie Disadvantages

 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 cookie information?

1. we can clear the cookie information from the client machine in the cookie folder

2. Until the cookie expires userInfo.Expires = DateTime.Now.AddHours (1); It will clear the cookie in one hour.

0
source

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

 System.Web.HttpContext.Current.Response.addCookie(cookieobject); 
-1
source

Source: https://habr.com/ru/post/917943/


All Articles