WebForm---Login status hold (cookies built-in objects)

Source: Internet
Author: User

Login status remains:

First do a login interface, click the login button

 protected  void  Page_Load (object   sender, EventArgs e) {button1.click  + = Button1_click;//  login button press two times tab    void  button1_click ( sender, EventArgs e) { //  first remove the username and password to the database class 
     bool  isOK = new  
    
      USERSDA ().        Select (Textbox1.text,textbox2.text); 
     if   (isOK) { 

If you can get on board, save the name in the cookie object before jumping to the page to record the user name
response.cookies["User"]. Value = TextBox1.Text;
if (checkbox1.checked)
{
response.cookies["User"]. Expires=datetime.now.adddays (15);
}

Response.Redirect ("main.aspx");//Jump page

        }       

When jumping from the login interface to the main interface, put a lable in the main interface to display the user name;

Login Front Code: <asp:checkbox id= "CheckBox1" runat= "server"/><label for= "CheckBox1" > Remember login status 15 days </label ><br/><br/>

Main Interface Background code:
protected voidPage_Load (Objectsender, EventArgs e) { //first of all, take the value of cookies if(request.cookies["User"] !=NULL)//not empty, with a value{Label1.Text= request.cookies["User"]. Value +", you are welcome"; } Else{Response.Redirect ("denglu.aspx"); }

There is a login to sign out:

A button is placed in the main interface: Log out and clear the cookies.

 button1.click += button1_click;  void  button1_click (object  Span style= "color: #000000;" > sender, EventArgs e) { //  1, clear cookies< /span> response.cookies[ "]. Expires = DateTime.Now.AddDays (-1    ) ; Only negative numbers  //  2, refresh the page, or skip to the login page  Resp Onse. Redirect (  " ); }   


Login status remains:
Cookies

A piece of text saved on the hard drive of the user's computer

The HTTP protocol includes a browser that allows the site to temporarily store data in the form of cookies on the user's computer.

If no save time is set, session cookies
1, if you do not refresh the page in 20 minutes, then this cookie will be automatically deleted
2, when the current access connection is interrupted, such as closing the browser, then the cookies will be automatically deleted

Role:
Keep the user's login status

WebForm---Login status hold (cookies built-in objects)

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.