ASP.net an instance of an action cookie

Source: Internet
Author: User
Tags datetime decrypt send cookies trim

  This article mainly introduces a simple example of the asp.net operation Cookie, and a friend in need can refer to the

The code is as follows:  protected void Page_Load (object sender, EventArgs e)     {        if (! IsPostBack)         {            HttpCookie cookie = request.cookies["L Ogininfo "];               if (cookie!= null)             {  &N Bsp             CB_REPWD. Checked = true;                 Tb_loginname.text = Uihelper.decrypt (cookie["LoginName"]);                 TB_PWD. Attributes.Add ("Value", Uihelper.decrypt (cookie["pwd"));                         else         &NB Sp   {                TB_PWD. Attributes.Add ("Value", "Hu Jintao");            }          }     }       protected void Btn_login_click (object sender, EventArgs e)     {  &NB Sp       if (uihelper.getstring (session["Code"]). ToUpper ()!=this.tb_code. Text.trim (). ToUpper ())         {            Ojavascript.alert ("Incorrect validation code input");   & nbsp         return;        }           Netuserservice nerservice = contextfactory.getobject<n Etuserservice> ("Netuserservice");         String sql = string. Format ("from Netuser where Loginname= ' {0} '", Tb_loginName.Text.Trim ());         ilist<netuser> list = nerservice.find (SQL);         if (list. Count > 0)         {            if (list[0). Status = = 2             {                OJAVASCRIpt. Alert ("The account has been deactivated, please contact the Administrator");                 return;                     {          if (Nerservic E.login (Tb_loginName.Text.Trim (), Osecurity.getpwdencrypt (TB_PWD). Text.trim ()))         {            HttpCookie cookie = request.cookies ["Logininfo"];             if (cb_repwd. Checked)             {                if (cookie = = NULL)                 {                & nbsp   HttpCookie cookielogin = new HttpCookie ("Logininfo");                     setcookies (cookielogin,7);                                 Else &NBSp               {                    I F (Uihelper.decrypt (request.cookies["Logininfo"] ["LoginName"])!= Tb_loginName.Text.Trim ())                     {                    & nbsp   Setcookies (Cookie, 7);                                   & nbsp {           }             else         & nbsp   {                setcookieexpires (cookie);         &NB Sp                              Ojavascript.redi Rect ("/");        }         else       &NBSP {            Ojavascript.alert ("Incorrect username or password");        }    }       #region Cookie    ///<summary>    ///setting cookies    ///</s ummary>    ///<param name= "Cookielogin" >httpcookie cookielogin</param>    ///< param name= "Expires" > Validity </param>     private void setcookies (HttpCookie cookielogin, int Expires) & nbsp   {        Cookielogin. values["loginName"] = Uihelper.encrypt (Tb_loginName.Text.Trim ());         Cookielogin. values["pwd"] = Uihelper.encrypt (tb_pwd. Text.trim ());         Cookielogin. Expires = DateTime.Now.AddDays (Expires);         Response.appendcookie (cookielogin);    }      ///<summary>    ///setting cookie expiration    ///</summary> & nbsp  ///<param name= "Cookielogin ">httpcookie cookielogin</param>     private void Setcookieexpires (HttpCookie Cookielogin)     {        if (cookielogin!= null)         {  &NB Sp         Cookielogin. Expires = DateTime.Now.AddDays (-1);             Response.appendcookie (cookielogin);        }     {    #endregion       Setting up a cookie is actually modifying the cookie and must be reset to the customer Send cookies   request.cookies cookies, response,cookies add cookies   Although simple, but do not understand the error is very easy  
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.