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.