C#-webform-session, cookie-login authentication (skip to login interface), hide Address bar value

Source: Internet
Author: User
Tags server memory

Post Pass value (invisible Pass value) Get Pass value (visible pass value)

Session-Global variable group

Storage location: Service side

Function: As long as there is content, then all the C # side of this site can access to this variable--object type

Format:

Web1 Background code:

 Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; }    //button click event    voidButton1_Click (Objectsender, EventArgs e) {        strings =TextBox1.Text; session["un"] =s; }}

The Session is a "group" of type object, which can put anything, string, int, array, etc.

use the first to determine whether the session is null, not empty when the assignment (Lee brainstorming Lxc)

WEB2 Background code:

 Public Partial class default2:system.web.ui.page{    protectedvoid Page_Load (object  sender, EventArgs e)    {        if (session["un"null)        {            = session["un"]. ToString ();}}    }

Benefits: Secure, convenient, unlimited global access

Disadvantage: Consumes server memory resources (each user is a separate channel, each channel has a number of sessions, if many people access, it will cause server crashes)

Life cycle: 20 minutes

When the Web page opens, a new session will open and the session will be 20 minutes, automatically emptied at the end, and will be re-timed if the session is re-read.

When the page is closed, the session will not be canceled, still in the countdown, and then re-open the page, will open a new session, the old session is still in the background timer

Note: 1, do not large amounts of data in the session, try not to put the collection

2, the temporary session after the useless, remember to empty (Lee brainstorming Lxc)

Cookie-"Global variable Group"

Storage location: On client computer hard disk

Function: Save data information, only save string, global access--string type

Format:

Web1 Background code:

 Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; }    //button click event    voidButton1_Click (Objectsender, EventArgs e) {        strings =TextBox1.Text; response.cookies["un"]. Value =s; }}

A cookie is a string type of "group" that can only hold a string type, stored as a text document string type somewhere in the client.

The use of the first to determine whether the cookie is null, not empty when the assignment (Li brainstorming lxc)

WEB2 Background code:

 Public Partial class default2:system.web.ui.page{    protectedvoid Page_Load (object  sender, EventArgs e)    {        if (request.cookies["un"]!=null          {            = request.cookies["un"]. Value;}}    }

Pros: Do not consume server resources, global access, only strings can be saved

Disadvantage: Consume computer resources, unsafe (cookies will be crawled), unreliable (users can delete at any time)

  Note: Do not use cookies to save passwords (user names will be assigned to cookies after successful login)

Category: Session cookies and persistent cookies

Life cycle:

1, session Cookie-20 minutes

A new cookie opens when the Web page opens, and the cookie will be available for 20 minutes, automatically emptied at the end, and will be re-timed if the cookie is re-read

Delete method:

(1) Browser Close, this visit has been interrupted

(2) No new submissions, 20 minutes after expiration

(3) Manual removal of browser cookies

2. Persistent cookie-can set the time

Delete method:(Lee brainstorming Lxc)

(1) Code settings expire

(2) Manual removal of browser cookies

How is persistent cookie made?

Foreground interface-keeps a persistent cookie when the check box is selected:

Background code:

 Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; }    //button click event    voidButton1_Click (Objectsender, EventArgs e) {        strings =TextBox1.Text; if(s = ="ADIMN"&& TextBox2.Text = ="1234") {response.cookies["un"]. Value =s; //set [Persistent cookie] time when selected            if(checkbox1.checked) {response.cookies["un"]. Expires = DateTime.Now.AddDays (3); } Response.Redirect ("default2.aspx"); }    }}

How do I delete a persistent cookie?

Background code:

 Public Partial classdefault2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {        if(request.cookies["un"]!=NULL) {Label1.Text= request.cookies["un"].        Value; } Button1.Click+=button1_click; }    //Click the button to clear the cookie and return to the login screen    voidButton1_Click (Objectsender, EventArgs e) {response.cookies["un"]. Expires = DateTime.Now.AddDays (-Ten); Response.Redirect ("Default.aspx"); }}

Extension one:

No login is unable to jump to the main page-login verification-do not put in IsPostBack

 Public Partial classdefault2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {        if(request.cookies["un"] !=NULL) {Label1.Text= request.cookies["un"].        Value; }        //Login Verification: Skip to login page if not logged in        Else{Response.Redirect ("Default.aspx"); } Button1.Click+=button1_click; }    //Click the button to clear the cookie and return to the login screen    voidButton1_Click (Objectsender, EventArgs e) {response.cookies["un"]. Expires = DateTime.Now.AddDays (-Ten); Response.Redirect ("Default.aspx"); }}

Extension two:

After jumping to the main page, the address bar still shows the address of the login interface.

Features: Use the address bar to pass values, but do not display the contents of the value in the Address bar (Li brainstorming lxc)

 Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; }    //button click event    voidButton1_Click (Objectsender, EventArgs e) {        strings =TextBox1.Text; if(s = ="ADIMN"&& TextBox2.Text = ="1234") {response.cookies["un"]. Value =s; //Response.Redirect ("default2.aspx");Server.Transfer ("default2.aspx"); }    }}

C#-webform-session, cookie-login authentication (skip to login interface), hide Address bar value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.