通过登入把用户信息和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(, CurrentId, DateTime.Now, DateTime.Now.AddDays(), 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 > )
{
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 = ;
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();
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()); }
else
{
context.User = user;
HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
cookie.Expires = new DateTime(, , );
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; }
}
}

redis + cookies 实现持久登入的更多相关文章

  1. Django,COOKIES,SESSION完成用户登入

    1.urls.py """Django_cookie_session URL Configuration The `urlpatterns` list routes UR ...

  2. python获取淘宝登入cookies

    重点:去新浪微博登入接口登入 一.代码 # coding=utf-8 import requests from selenium.webdriver.common.by import By from ...

  3. [ASP.NET MVC] ASP.NET Identity登入技术应用

    [ASP.NET MVC] ASP.NET Identity登入技术应用 情景 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授权等等机制.在ASP.NET I ...

  4. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  5. Python爬虫-尝试使用人工和OCR处理验证码模拟登入

    刚开始在网上看别人一直在说知乎登入首页有有倒立的汉字验证码,我打开自己的知乎登入页面,发现只有账号和密码,他们说的倒立的验证码去哪了,后面仔细一想我之前登入过知乎,应该在本地存在cookies,然后我 ...

  6. uploadify Cookie 验证登入上传问题

    上传文件时必须验证是否已登入. 当用FormsAuthentication做登入,使用FormsAuthentication.FormsCookieName进行验证是否已登入即可. <scrip ...

  7. Spring Boot SSO单点登入

    https://github.com/ITDragonBlog/daydayup/tree/master/SpringBoot-SSO 流程图: 1: Redis 保存用户信息 到Redis(KEY- ...

  8. PHP登入网站抓取并且抓取数据

    有时候需要登入网站,然后去抓取一些有用的信息,人工做的话,太累了.有的人可以很快的做到登入,但是需要在登入后再去访问其他页面始终都访问不了,因为他们没有带Cookie进去而被当做是两次会话.下面看看代 ...

  9. [转]解決 IE10 瀏覽器無法使用 ASP.NET 表單驗證登入的問題

    今天凌晨在客戶端上線,當程式佈署到正式機後發現我們的網站唯獨只有 IE10 瀏覽器無法成功登入,任何其他瀏覽器版本或使用較低的 IE 版本都可以正常登入,使用 IE 相容性檢視也都可以正常登入,想說會 ...

随机推荐

  1. L1-Day14

    今天是周日,不用交作业,但是需要把这一周的知识点复习总结 做个思维导图吧

  2. spring boot 框架设计步骤

    spring boot 框架设计步骤: 1.poem.xml配置 2.application.yml配置 3.entiry实体 4.realm.Myrealm extends AuthorizingR ...

  3. Git开发工作流

    1.1 master分支 主分支,产品的功能全部实现后,最终在master分支对外发布. 1.2 develop分支 开发分支,基于master分支克隆,产品的编码工作在此分支进行. 1.3 rele ...

  4. Linux input系统数据上报流程【转】

    转自:https://segmentfault.com/a/1190000017255939 作为鸡生蛋系列文章,这里主要关注Linux input系统,主要为触摸事件上报流程. 读该文章最好有对li ...

  5. mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”)的处理方法

    环境 CentosOS 6.5 ,已安装mysql 情景 root密码忘记,使用普通用户无法登录 解决 问题一 无法使用mysql命令 参考文章:https://www.cnblogs.com/com ...

  6. springboot动态多数据源

    参考文章:https://www.cnblogs.com/hehehaha/p/6147096.html 前言 目标是springboot工程支持多个MySQL数据源,在代码层面上,同一个SQL(Ma ...

  7. c#--Redis帮助类

    最近一直在忙公司的一下项目,也没有太多时间写,所以就分享出所用redis帮助类 using Newtonsoft.Json; using StackExchange.Redis; using Syst ...

  8. (转)python 开发 sqlite 绝对完整

    '''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...

  9. Mysql exists 与 in

    今天公司同事反馈一个SQL语句删除数据删除了一个小时,还没有删除完,强制中断. 第一眼看到 exists 的时候,脑子里要有这么个概念: Oracle exists 的效率比in 高.而Mysql 则 ...

  10. 强连通分量Kosaraju

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...