ASP.net Simple example of manipulating cookies _ practical Tips

Source: Internet
Author: User
Tags decrypt set cookie

Copy Code code as follows:

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", "Hu Jintao");
}

}

}

protected void Btn_login_click (object sender, EventArgs e)
{

if (uihelper.getstring (session["Code"]). ToUpper ()!=this.tb_code. Text.trim (). ToUpper ())
{
Ojavascript.alert ("Incorrect validation code input");
Return
}

Netuserservice Nerservice = contextfactory.getobject<netuserservice> ("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 (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);
}
Else
{
if (Uihelper.decrypt (request.cookies["Logininfo"] ["LoginName"])!= Tb_loginName.Text.Trim ())
{
Setcookies (Cookie, 7);
}
}
}
Else
{
Setcookieexpires (cookie);
}


Ojavascript.redirect ("/");
}
Else
{
Ojavascript.alert ("Incorrect username or password");
}
}

    #region Cookies
   ///<summary>
   ///Setting cookies
   ///</summary>
   ///<param name= "Cookielogin" >httpcookie Cookielogin</param>
   ///<param name= "Expires" > Validity </param>
     private 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);
   }

<summary>
Set Cookie Expiration
</summary>
<param name= "Cookielogin" >httpcookie cookielogin</param>
private void Setcookieexpires (HttpCookie cookielogin)
{
if (Cookielogin!= null)
{
Cookielogin. Expires = DateTime.Now.AddDays (-1);
Response.appendcookie (Cookielogin);
}
}
#endregion

Setting a cookie expiration is actually modifying the cookie, and you must resend the cookie to the client

Request.Cookies cookies, response,cookies add cookies

It's simple, but it's easy to make mistakes without knowing it.

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.