Asp.net operation cookie

Source: Internet
Author: User

Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {HttpCookie cookie = Request. Cookies ["loginInfo"]; if (cookie! = Null) {cb_repwd.Checked = true; tb_loginName.Text = UIHelper. decrypt (cookie ["loginName"]); tb_pwd.Attributes.Add ("value", UIHelper. decrypt (cookie ["pwd"]);} else {tb_pwd.Attributes.Add ("value", "******") ;}} protected void btn_login_Click (object sender, eventArgs e) {if (UIHelper. getString (Session ["Code"]). toUpper ()! = This. tb_code.Text.Trim (). ToUpper () {oJavaScript. Alert ("Incorrect verification code input"); return;} NetUserService NerService = ContextFactory. GetObject
 
  
("NetUserService"); string SQL = string. Format ("from NetUser where LoginName = '{0}'", tb_loginName.Text.Trim (); IList
  
   
List = NerService. find (SQL); if (list. count> 0) {if (list [0]. status = 2) {oJavaScript. alert ("Account Disabled, please contact the Administrator"); return ;}} if (NerService. login (tb_loginName.Text.Trim (), oSecurity. getPwdEncrypt (tb_pwd.Text.Trim () {HttpCookie cookie = Request. cookies ["loginInfo"]; if (cb_repwd.Checked) {if (cookie = null) {HttpCookie cookielogin = new HttpCookie ("loginInfo"); setCookies (cookielogin, 7);} el Se {if (UIHelper. Decrypt (Request. Cookies ["loginInfo"] ["loginName"])! = Tb_loginName.Text.Trim () {setCookies (cookie, 7) ;}} else {setCookieExpires (cookie);} oJavaScript. redirect ("/");} else {oJavaScript. alert ("incorrect user name or password") ;}# region cookie ///
   /// Set cookie //////
   HttpCookie cookielogin///
   Validity PeriodPrivate void setCookies (HttpCookie cookielogin, int Expires) {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 );}///
   /// Set cookie invalidation //////
   HttpCookie cookieloginPrivate void setCookieExpires (HttpCookie cookielogin) {if (cookielogin! = Null) {cookielogin. Expires = DateTime. Now. AddDays (-1); Response. AppendCookie (cookielogin) ;}# endregion
  
 

Setting cookie expiration is actually modifying the cookie. You must resend the cookie to the client.

Request. Cookies: cookie, response, and cookies are used to add Cookies.

Although simple, but not familiar with it, it is easy to make mistakes


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.