Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Idea about cookie session
|
Sessional Cookies • Sessional Cookies, exit in the memory space of browser. When the browser is closed, all session cookies added to the browser are lost. Note: • You use the Response.Cookies collection to create & modify Cookies. • You use the Request.Cookies collection to retrieve cookie’s value.
How to Add Cookie values on to Sessional Cookie Button1_Click • HttpCookie hc=new HttpCookie(this.textBox1.Text,this.textBox2.Text); • Response.Cookies.Add(hc);
How to Read Cookie values from Sessional Cookie Button2_Click • foreach(string key in Request.Cookies) { this.label1.Text +=””+key+Request.Cookies[key].value+””;
}
Persistent Cookie • A persistent cookie is saved as a text file in the file system of the client computer, in the following location C:\Documents and Settings\Administrator\Cookies
Ex: Button_Click HttpCookie hc=new HttpCookie(this.textBox1.Text, this.textBox2.Text); hc.Expires=ConvertToDateTime(“12/22/2010”); Response.Cookies.Add(hc); //Cookie is added by Response Collection HttpCookie MyCookie=Request.Cookies.Get(this.textBox1.Text); //Retrieve the details of the cookie named which is present inside texbox1 in the HttpCookie object MyCookie. Response.Write(“Cookie :”+MyCookie.Name+” ”); Response.Write(“Cookie Expires :”+MyCookie.Expires+” ”);
Advantage Of Cookie
• A Cookie is stored on the client computer & can be read by the server after a request for a page is posted. Therefore, no server resource is involved in maintaining the cookie. • A cookie is a text based file that contains key – value pairs. Therefore, it is easy to create and manipulate cookies. • A cookie can either expire when the browser session ends or exist indefinitely on the client computer, subject to the expiration rules on the client. Disadvantage Of Cookie
• Cookie has limited size i.e upto 4 KB(4096 B), So you can’t store a large amount of data in cookie. • Users can tamper with cookies because cookies are stored on the client computer. • Users can disable cookies to prevent them from being stored on the hard disk of their computer. If a user denies permission for cookies, an ASP.NET web application can’t store cookies on the client computer
Server Side State management:
• There are 3 different ways are there, by which sate can be managed at server side. o Session o Application o Server Session:
• In ASP.Net, session state is used to store session – specific information for a web site. • Session is the time period for a particular user which always created at Server but is available in the client through Cookies • A unique 120 – bit SessionID string containing ASCII characters identifies and tracks each active ASP.Net session. • The SessionID strings are communicated across client – server requests either by means of an HTTP cookie. • By this SessionID, server can easily recognize that which particular user is giving “n” nos. of requests • Syntax For Session: o Session[string name]=value; o For Retrieving values from Session ? Response.Write(Session[string name]); • Note: o By default a session variable is active for 20 minutes without any user interaction. • When a user first request a page from an ASP.NET Web site, the web site automatically adds a session cookie to the client browser. This cookie name is ASP.NET_SESSIONID. This cookie is used for the remainder of the user’s visit to the web site. • Syntax for getting the value of SessionID o Response.Write(Session.SessionID); • How to stop explicitly a user Session o Session.Abandon() • Handling Session Events o Session State has the following events that you can capture to manipulate an Asp.Net web application. ? Session_Start : This is raised when a user requests the first page from a web site ? Session_End : This is raised when the user Session ends • Example
Step 1 : Add Global.asax file on to the project, then write down the following codes. Application_Start Application[“Lakshya”]=0; Step 2 : Session_Start Session[“NET”]=0; Application[“Lakshya”]=(int) Application[“Lakshya”] + 1; Session[“NET”] = (int) Session[“NET”]+1; Step 3 : Session_End Application[“Lakshya”]=(int) Application[“Lakshya”] - 1; Session[“NET”] = (int) Session[“NET”]-1;
Step 4 :Page_Load Response.Write(“Application Counter is : “+Application[“Lakshya”]); Response.Write(“\n Session Counter is : “+Session[“NET”]);
Application • ASP.Net provides application state as a means of storing global application – specific information. • The Information in the application state is stored in a key – value pair and is used to maintain data consistency between server round trips and between pages. • Application state is created when each browser request is made for a specific URL. After an application state is created, the application – specific information is stored in it. • All information stored in the application state is shared among all pages of the web application by using the HttpApplicationState class. • Syntax for storing information inside the Application o Application[String Name]=value; o Ex: ? Application[“LAKNET”]=”Good Morning”; ? How to Retrieve • Response.Write(Application[“LAKNET”]);
• You can also add complex objects, such as Collections & DataSets. o Ex: ? DataSet ds=new DataSet(); ? Application[“DSET”]=ds; • How to remove an Application variable from the Application state o Application.Remove(“ApplicationStringName”); ? Ex: Application.Remove(“LAKNET”); • Example o Step 1 : In Global.asax ? Application_Start • Application[“LAKCOUNTER”]=0; o Step 2 : Page_Load Application.Lock(); Application["LAK"] = (int)Application["LAK"] + 1; Response.Write(Application["LAK"]); Application.UnLock(); o Note: ? Lock() • This method ensures that the variable LAK can’t be simultaneously modified by another thread.
? UnLock() • This method is called to release the imposed lock on the application state variable LAK
o Advantages of Application State ? Application state is easy to use and is consistent with other .NET Framework classes. ? Storing information in application state involves maintaining only a single copy of the information because application state is accessible to all the pages in an application. o DisAdvantage ? The global data stored in an application state is lost when the web server process containing the application state fails due to server crash, upgrade or shutdown
plz send ur suggestion to :sudhirbehera09@gmail.com
|
|
Responses to the resource: "Idea about cookie session"
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|