. NET MVC uses cookies to log off information after automatic login exits

Source: Internet
Author: User

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

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.