difference between cookies and sessions in ASP.NET

Posted by Rojasamala under ASP.NET on 7/31/2013 | Points: 10 | Views : 1319 | Status : [Member] | Replies : 6
what is the difference between cookies and sessions in ASP.NET?
Aspdotnetroja

Thanks
S Roja
http://aspdotnet-roja.blogspot.com



Responses

Posted by: Ssj_Kumar on: 7/31/2013 [Member] Starter | Points: 25

Up
0
Down
Please read the below link you will get lot of knowledge in Cookies and sessions

http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies
http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net

below link provide you the stright forward answer

http://php.about.com/od/learnphp/qt/session_cookie.htm

Regards,
Jayakumar Selvakani

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/31/2013 [Member] Starter | Points: 25

Up
0
Down
please find the below link
http://www.dotnetfunda.com/interview/exam3460-what-is-the-difference-between-cookie-and-session-in-aspnet.aspx

Regards,
Jayakumar Selvakani

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: hareeshkumarkr-16190 on: 7/31/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Fnd the bellow link

http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 7/31/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
refer this link it might be helpful to you
http://www.codeproject.com/Questions/309565/Difference-between-session-viwestate-and-cookies
http://in.answers.yahoo.com/question/index?qid=20100604025904AAD1J7s

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 7/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Cookies: - Cookies are little pieces of information that a server stores on a browser. They are of two types


Temporary cookie
Persistent cookie



Temporary cookie: - They are also known as session cookies. These are volatile in nature. When the browser is shutdown they are erased.



Persistent cookie:- These may be called as permanent cookies. These are especially saved in files. It may remain for a month or year.


Properties of cookies

Some properties of cookie

Name: - represent the name of cookie.

Name value: - represent a collection of key values of cookie

Domain: - represent the domain associated with a specific cookie.

Path: - the path associated with a cookie.

Expires: - expired time of cookie.

Hashkey: - identifies whether the cookie is a cookie dictionary.

Secure: - specifies whether the cookie is to be sent in an encrypted connection or not.


Working with cookies

Cookies are send back and forth between a browser and server. The server creates a cookie by using the set cookie header. Subsequent request from the browser is send to the server in cookie header. Suppose the server create a cookie



Set cookie: username=rajkumar; path=1;domain=abc.com; expires=Friday, 19th august 20116:30:00GMT

The server creates the cookie named as username containing the user information. Path identifies any requested page to the website, domain identifies the specific website abc.com, expired identifies that this cookie would be expired on the specific data mentioned.

After the server creates the cookie the response is send to the server from the browser for each subsequent request to the webpage. The reader sends in the format cookie: username=raj+kumar

The server is using set cookie reader. The browser is using reader.


Creation and reading session cookies

The session cookies can be created by using the response object and can be read by using the request object. The cookies properties of the response and request objects contains instances of Httpcookies collection class. Each cookie is created by using Httpcookie class.


For creating cookies

Dim cookies1 As New HttpCookie("raj", "kumar")

Response.Cookies.Add(cookies1)
For reading cookies

Response.Write(request.cookies(cookies1).value)


Program



Partial Class _Default

Inherits System.Web.UI.Page



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

'For creating cookies



Dim cookies1 As New HttpCookie(TextBox1.Text, TextBox2.Text)

Response.Cookies.Add(cookies1)

clear()

End Sub



Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

'For reading cookies

Dim s As String

For Each s In Request.Cookies

Label1.Text += "<li>" & s

Next

End Sub



Sub clear()

TextBox1.Text = ""

TextBox2.Text = ""

End Sub

End Class


Persistence cookies

It is saved in a config file.it has a specific expiration date. It is especially used to store information about userid, name of authenticated user.


For creating persistence cookie

Dim pcookie1 as New HttpCookie("user name"."ravi kumar")

Response.Cookies.Add(pcookie1)

pcookie1.expires= #19/06/2011#




For reading persistent cookie

Response.Write(Request.cookies("user name").Value)


Limitation of cookies: -

1.A cookie can store maximum of 4kb of data.

2.A cookie can contain only string values. It can contain objects like dataset object.

3.A cookie is browser dependent.

4.Persistent cookie may be deleted before expiration date by browser.


If this post helps you mark it as answer
Thanks

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 7/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead. Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file. Markup code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Sessions</title>
</head>
<body runat="server" id="BodyTag">
<form id="form1" runat="server">
<asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged">
<asp:ListItem value="White" selected="True">Select color...</asp:ListItem>
<asp:ListItem value="Red">Red</asp:ListItem>
<asp:ListItem value="Green">Green</asp:ListItem>
<asp:ListItem value="Blue">Blue</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>

And here is the CodeBehind:

using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["BackgroundColor"] != null)
{
ColorSelector.SelectedValue = Session["BackgroundColor"].ToString();
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
}
}

protected void ColorSelector_IndexChanged(object sender, EventArgs e)
{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
Session["BackgroundColor"] = ColorSelector.SelectedValue;
}
}


http://asp.net-tutorials.com/state/sessions/
http://asp-net-example.blogspot.in/2009/01/aspnet-session-example-how-to-use.html

If this post helps you mark it as answer
Thanks

Rojasamala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response