Use Cookies for login verification

Source: Internet
Author: User
Tags datetime trim

Nnd use the framework to do the backstage, the session is always lost. Helpless, using cookies to put.

HttpCookie MyCookie = new HttpCookie ("MyCookie");
MYCOOKIE.VALUES.ADD ("AdminName", This.txtName.Text.Trim (). ToString ());
MYCOOKIE.VALUES.ADD ("Pwd", This.txtPwd.Text.Trim (). ToString ());
Mycookie.expires = DateTime.Now.AddHours (3); Cookie Save time

Response.appendcookie (MyCookie);

Verify

if (request.cookies["MyCookie"] = = null)
{
Response.Redirect ("~/admin/login.aspx");
}

Take value

This.lblName.Text = request.cookies["MyCookie"]. values["AdminName"]. ToString ();

Secure Exit or logoff

response.cookies["MyCookie"].                      Expires = DateTime.Now.AddDays (-1); Make a cookie expire
Page.ClientScript.RegisterStartupScript (this. GetType (), "", "<script>parent.location.href= ' Login.aspx ';</script>");

Spare (fix Chinese garbled):

Code: MYCOOKIE.VALUES.ADD ("AdminName", Httputility.urlencode (This.txtName.Text.Trim (). ToString (), System.Text.Encoding.GetEncoding ("gb2312"));

Decode: This.lblName.Text = Httputility.urldecode (request.cookies["MyCookie"]. values["AdminName"]. ToString (), System.Text.Encoding.GetEncoding ("gb2312"));

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.