1.登录Action中将用户信息加入到cookie中:
HttpCookie cookie = new HttpCookie("useridMsg");
cookie.Expires= DateTime.Now.AddDays(7);
cookie["userid"] = 你的用户id;
Response.Cookies.Add(cookie);
2.创建类CookieFilter并继承FilterAttribute和IActionFilter
实现抽象IActionFilter的方法 OnActionExecuted和OnActionExecuting
在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.添加全局过滤器CookieFilter
在global内注册全局过滤器 RegisterGlobalFilters方法内添加代码: filters.Add(new CookieFilter());
4.退出Action
a.清除session Session.RemoveAll();
b.清除cookie
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.纯手打 可能有错 代码也没来得及优化和考虑安全问题
版权声明:本文为博主原创文章,未经博主允许不得转载。