Asp. NET, remember the user name and password (with source download)--asp.net

Source: Internet
Author: User
Tags set cookie

What you need to know: what the instance needs to do is create the cookie object and read the cookie object data, create a cookie through the cookies attribute of the response object, and read the cookie object's data through the request object's cookies.

Case thinking: Create and save a cookie in a textbox for a certain amount of timelogin.aspx:
1<%@ Page language="C #"autoeventwireup="true"codefile="Login.aspx.cs"inherits="Login"%>2 3<! DOCTYPE html>4 5"http://www.w3.org/1999/xhtml">6"Server">7<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>8<title> Remember user name and password when logging in </title>9Ten<body> One<form id="Form1"runat="Server"> A<div> -User name <asp:textbox id="TextBox1"runat="Server"></asp:textbox><br/> -Secret &nbsp;&nbsp;&nbsp; Code <asp:textbox id="TextBox2"runat="Server"></asp:TextBox><br/> the<asp:checkbox id="CheckBox1"runat="Server"/> Remember user name and password <br/> -<asp:button id="Button1"runat="Server"text="Login"onclick="Button1_Click"/>&nbsp;&nbsp; -<asp:button id="Button2"runat="Server"text=" Reset"onclick="Button2_click1"/> -</div> +</form> -</body> +Login.aspx.cs:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8  Public Partial classLogin:System.Web.UI.Page9 {Ten     //What to do when the page loads Start One     protected voidPage_Load (Objectsender, EventArgs e) A     { -         //The first load does not satisfy the condition, then all satisfies (except the cookie to the time), the cookie data is exported to the TextBox -         if(request.cookies["username"]!=NULL&&request.cookies["Password"]!=NULL) the         { -TextBox1.Text = request.cookies["username"]. Value.tostring (); -TextBox2.Text = request.cookies["Password"]. Value.tostring (); -         } +     } -     //content to be executed when the page loads end +     //implementation of the reset button function Start A     protected voidButton2_click1 (Objectsender, EventArgs e) at     { -          This. Findbutton ( This); -     } -     Private voidFindbutton (Control c) -     { -         if(C.controls! =NULL) in         { -             foreach(Control xinchc.controls) to             { +                 if(x isSystem.Web.UI.WebControls.TextBox) -                 { the((System.Web.UI.WebControls.TextBox) x). Text =""; *                 } $ Findbutton (x);Panax Notoginseng             } -         } the     } +     //implementation of the Reset button's function End A     //The implementation of the login button's function satrt the     protected voidButton1_Click (Objectsender, EventArgs e) +     { -         if(checkbox1.checked) $         { $             //Create cookie object, save cookie data, set cookie save time -response.cookies["username"]. Value =TextBox1.Text; -response.cookies["username"]. Expires = DateTime.Now.AddSeconds (Ten); theresponse.cookies["Password"]. Value =TextBox2.Text; -response.cookies["Password"]. Expires = DateTime.Now.AddSeconds (Ten);WuyiResponse.Redirect ("test.aspx"); the         } -     } Wu     //implementation of the login button's function End -}

source file Download: Remember the user name and password when you log on in ASP. ziparticle is the author of the original, reproduced please indicate the source, thank you for your cooperation!

Asp. NET, remember the user name and password (with source download)--asp.net

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.