博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
redis + cookies 实现持久登入
阅读量:5334 次
发布时间:2019-06-15

本文共 3370 字,大约阅读时间需要 11 分钟。

 

通过登入把用户信息和token加载到redis中去,

将token和部分用户信息存储在cookie中,

下次登入时 判断cookie的token在redis中是否存在,

存在就把用户信息加载出来自动登入。

 

public class LoginFormPrincipal : IPrincipal{private IIdentity _identity;public LoginFormPrincipal(LoginFormIdentity loginFormIdentity){if (loginFormIdentity == null){throw new ArgumentNullException("loginFormIdentity");}_identity = loginFormIdentity;}public IIdentity Identity{get{return _identity;}}public bool IsInRole(string role){throw new Exception("");}public bool SignOut(){FormsAuthentication.SignOut();HttpContext.Current.Session.Abandon();return true;}public static void SignIn(string CurrentId, string Token, int expiration){FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, CurrentId, DateTime.Now, DateTime.Now.AddDays(1), true, Token);string cookieValue = FormsAuthentication.Encrypt(ticket);HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue);cookie.HttpOnly = true;cookie.Secure = FormsAuthentication.RequireSSL;cookie.Domain = FormsAuthentication.CookieDomain;cookie.Path = FormsAuthentication.FormsCookiePath;if (expiration > 0){cookie.Expires = DateTime.Now.AddMinutes(expiration);}HttpContext context = HttpContext.Current;if (context == null){throw new InvalidOperationException();}context.Response.Cookies.Remove(cookie.Name);context.Response.Cookies.Add(cookie);}private static FormsAuthenticationTicket TryParseAuthenticationTicket(HttpRequest request){if (request == null){throw new ArgumentNullException("request");}HttpCookie cookie = request.Cookies[FormsAuthentication.FormsCookieName];if (cookie == null || string.IsNullOrEmpty(cookie.Value)){return null;}try{return FormsAuthentication.Decrypt(cookie.Value);}catch{}return null;}private static LoginFormPrincipal TryParsePrincipal(HttpRequest request){FormsAuthenticationTicket ticket = TryParseAuthenticationTicket(request);if (ticket == null){return null;}int UserId = 0;if (!int.TryParse(ticket.Name, out UserId)){return null;}string Token = ticket.UserData;if (string.IsNullOrEmpty(Token)){return null;}return new LoginFormPrincipal(new LoginFormIdentity(UserId, Token));}public static void TrySetUserInfo(HttpContext context){if (context == null){throw new ArgumentNullException("context");}LoginFormPrincipal user = TryParsePrincipal(context.Request);if (user != null){HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];cookie.Expires = DateTime.Now.AddMinutes(20);context.Response.Cookies.Remove(cookie.Name);context.Response.Cookies.Add(cookie);context.User = user;string key = string.Format(RedisKeys.CurrentUser, user.Identity.Name + user.Identity.AuthenticationType);RedisBase.SetListExpire(key, DateTime.Now.AddMinutes(20));}else{context.User = user;HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];if (cookie != null){cookie.Expires = new DateTime(1970, 1, 1);context.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);context.Response.Cookies.Add(cookie);}}}}public class LoginFormIdentity : IIdentity{private string _userId;private string _token;public LoginFormIdentity(int UserId, string Token){_userId = UserId.ToString();_token = Token;}public string AuthenticationType{get { return _token; }}public bool IsAuthenticated{get { return true; }}public string Name{get { return _userId; }}}

 

转载于:https://www.cnblogs.com/chongerwangzi/p/7366110.html

你可能感兴趣的文章
转载Repository 和Unit of work的使用说明
查看>>
文件分割与合并
查看>>
redis中各种数据类型对应的jedis操作命令
查看>>
设置虚拟机安装的mysql访问权限及删除mysql
查看>>
vue项目中别个访问你的本地调试需要改东西
查看>>
SpringBoot集成Swagger2 以及汉化 快速教程
查看>>
读《奇点临近》
查看>>
[百科]sys/types.h
查看>>
DAY56-前端入门-javascript(三)
查看>>
Anaconda 环境中使用pip安装时候出现的一些问题
查看>>
POJ 3461 还是两种方法
查看>>
【译】SSH隧道:本地和远程端口转发
查看>>
win8.1安装Python提示缺失api-ms-win-crt-runtime-l1-1-0.dll问题
查看>>
mysql授权grant
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
【乱搞】【AOJ-574】爱就大声说出来
查看>>
RESTful记录-RESTful服务
查看>>
工程项目缺少C文件会怎么样?STM32 flasher
查看>>
Clojure编写一个阶乘程序 使用递归
查看>>
zbb20190703 搭建vue工程并配置idea开发工具
查看>>