C # use cookies for logon,

Source: Internet
Author: User

C # use cookies for logon,

First, what do we need to do?

After successfully logging on, we will jump to the main interface, and the logon button on the main interface will become the Avatar. The next time you open the webpage, You need to determine whether you have logged on. If you have a cookie, you do not need to log on.

1. After Successful Logon, the client requests the server

2. Pass the login information to the server

3. The server has this cookie, saves it to the cookie set, and then reports it to the client.

 

The background Controller code is as follows:

public ActionResult Login(){    HttpCookie cookie = new HttpCookie("userid", "12312");    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);    return Redirect("/Home/Catagory");}
Controller

The front-end Html code is as follows:

@ {Var cookie = System. web. httpContext. current. request. cookies. get ("userid"); if (cookie = null | string. isNullOrWhiteSpace (cookie. value) {<a href = "#"> log on </a>} else {<p> <span> @ (cookie. value) </span> </p> }}
Html

For example, I (client) took the ID card (information) by taking the train (http request) and handed you the ID card (server). After you get the ID card, then you give me a piece of paper (cookie), indicating that you have obtained your ID card. I took this paper and went back. Every time I told someone that I gave you my ID card, others didn't believe it, when I took out the paper, others would understand that if I lost the paper one day, others would ask again, and I would not be able to prove it again.

This is probably the process.

 

 

Note: This article is for reference only. It also has many flaws. The most important thing is not code, but logic.

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.