1. In the login action, add the user information to the cookie:
HttpCookie cookie = new HttpCookie ("useridmsg");
Cookies. Expires= DateTime.Now.AddDays (7);
cookie["userid"] = Your user ID;
RESPONSE.COOKIES.ADD (cookie);
2. Create class Cookiefilter and inherit FilterAttribute and Iactionfilter
Ways to implement abstract Iactionfilter onactionexecuted and onactionexecuting
To add logic in onactionexecuting:
if (system.web.httpcontext.current.request.cookies["Useridmsg"]!=null) {
if (System.Web.HttpContext.Current.Request.Cookies.Get ("useridmsg"). values["userid"]!=null) {
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get ("useridmsg");
filtercontext.httpcontext.session["MemberID"] = cookie. values["UserID"];
}
}
3. Add Global filter Cookiefilter
Register global filters within global registerglobalfilters add code inside the method: Filters. ADD (New Cookiefilter ());
4. Exit action
A. Clear Session Session.removeall ();
B. Clearing cookies
HttpCookie cookiemsg;
String CookieName;
for (int i=0;i<request.cookies.count;i++) {
CookieName = Request.cookies[i]. Name;
cookiemsg = new HttpCookie (cookiename);
Cookiemsg.expires = DateTime.Now.AddDays (-1);
RESPONSE.COOKIES.ADD (COOKIEMSG);
}
5. Pure hand play may be wrong code also did not have time to optimize and consider security issues
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
. NET MVC uses cookies to log off information after automatic login exits