Thursday, November 10, 2011

State Management:Cookie

In This Post I will show You what is Cookie,How to use it and what is advantage and Limitation of cookie..

What is Cookie?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web
server and browser. The cookie contains information the Web application can read whenever the user visits the site.Imagine that when users request a page from your site, www.Example.com, your application sends not just a page, but a cookie containing the date and time. When the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's hard disk.Later, the user requests a page from your site again. When the user enters the URL
www.contoso.com, the browser looks on the local hard disk for a cookie associated with the URL. If the cookie exists, the browser sends the cookie to your site along with the page request. Your
application can then determine the date and time that the user last visited the site. You might use the information to display a message to the user, check an expiration date, or perform any other useful function.

Limitation of Cookies?
Before we start on the mechanics of working with cookies, I will mention a couple of limitations of
working with cookies. Most browsers support cookies of up to 4096 bytes. That is plenty of space for storing a few values on the user's computers, but you would not want to try to store a dataset or some other potentially large piece of data in a cookie. More practically, you probably do not want to store a big collection of user information in a cookie. Instead, you would want to store a user number or other identifier. Then, when the user visits your site again, you would use the user ID to look up user details in a database. (But see Cookies and Security for some caveats about storing user information.)
Browsers also impose limitations on how many cookies your site can store on the user's computer. Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded. Some browsers also put an absolute limit, usually 300, on the number of cookies they will accept from all sites combined.

How To Add(write) Cookie...

Example
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

Read Cookie

string color=Request.Cookies["BackgroundColor"].Value;

Delete Cookie

HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = "green";
cookie.Expires =DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);


Store Multiple values in cookie and Read it.

HttpCookie userCookie = new HttpCookie("UserInfo");
userCookie["Country"] = "Italy";
userCookie["City"] = "Rome";
userCookie["Name"] = "Jones";
userCookie.Expires = DateTime.Now.AddDays(3);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie create successful!";
HttpCookie cookie = Request.Cookies["UserInfo"];
if (cookie != null)
{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label2.Text = "Cookie Found and read";
Label2.Text += "Name: " + name;
Label2.Text += "Country: " + country;
Label2.Text += "City: " + city;
}

Monday, November 7, 2011

Static and Dynamic web pages

We can broadly classify web sites and web pages into two categories:

1. Static web pages
2. Dynamic web pages

Static Web Pages



A static web page is a page which has the same content always.

In case of static web pages, content is written in the page itself as plain html. Until the author of the web page updates the content, the content remains the same in the static pages.

Static web pages are meant for providing information which does not change often. For example, visithttp://www.google.com/intl/en/about.html. This page is a static page. The content is always the same (until they update the content by uploading a new html file to the web server).

HTML files are used to create static web pages.

Dynamic Web Pages



Dynamic web pages get content from database. Content is NOT hard-coded in the page itself.

Dynamic pages are created using "serverside code" when the page is loaded every time.

An example for a dynamic page is this tutorial page itself. See the file name in the URL. The file name is "Tutorial8.aspx". We have used only one file to display any tutorial chapter. The chapter number is there as part of the URL (the chapter number is 8 for this chapter).

When you type the URL in the browser, this page is dynamically created from database, based on the chapter id in the URL. The content of each chapter is stored in our database, not in the file itself. When you access the page, our server side code will check what is the TutorialId in the URL. Based on the TutorialId, it will retrieve appropriate chapter content from the database and dynamically create the web page. (You can try to change the TutorialId in the URL to some very large number and try. The page will give an error because our code will fail to get the corresponding chapter content from the database). This entire site has only very few files (like index.aspx, Tutorial.aspx etc)

Dynamic web pages are created using technologies like ASP, ASP.NET, PHP etc.

HTML pages cannot be dynamic. All HTML files are static pages. If you want to write dynamic pages, you must use some technologies like ASP.NET.

Wednesday, November 2, 2011

How To use MD5 in asp.Net.

Introduction

Its quite simple to use MD5 in asp.Net.its a one type of Encryption Method.It is basically used for high security.because it is not possible to decrypt the encrypted string using MD5.MD5 is a one way function thats why it is impossible to decrypt it.

For Example..

This is .aspx code


Just click on image to see it well.



Now in aspx.cs page code....
Add namespace using System.Web.Security;

protected void btnEncrypt_Click(object sender, EventArgs e)
{
lblEncryptAns.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text,"MD5");

}

Now see the text of
lblEncryptAns and Encrypt again with the Output Result.Then You will get the Original string it will show you the different string.