通过登入把用户信息和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. LOJ #6539 奇妙数论题

    不想咕太久..就随便找个题更一下 LOJ#6539 题意 求题面里那个式子 题解 有一个常用的小式子 $$\sum_{x|a,x|b}\varphi(x)=\gcd(a,b)$$ 用这个式子直接对题面 ...

  2. 关于SVN报错 svn: E170013 E125006: contains invalid filesystem format option 'addressing logical'

    在使用svn的时候,遇到了这样的一个问题 首先我使用TortoiseSVN 右键创建的repository. 之后用IDEA,配置了1.9.4版本的SVN,去commit访问这个仓库 结果出现了以下的 ...

  3. ES7的async/await

    async 表示这是一个async函数,await只能用在这个函数里面. await 表示在这里等待promise返回结果了,再继续执行. await 后面跟着的应该是一个promise对象 awai ...

  4. [经验交流] CentOS7 docker 使用 overlay 存储

    关于 docker overlay 存储相比 device mapper 的优势,可以在 google 上搜索相关资料.在 ubuntu 16.04.2 上可以直接使用 overlay 存储,而在 C ...

  5. selenium模块

    一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被夹在 元素交互操作 其他 项目联 一 介绍 selenium最初是一个自动化测试的工具,而爬虫中使用它主要是为了解决requests无法直接 ...

  6. OPENSSL_Applink 错误

    原因 : 程序太老, 调用了参数为 FILE * 类型的 api. 解决方式: 1. windows exe, 可直接#include<openssl/applink.c>  // ext ...

  7. python学习第30天

    tcp协议的socket server 并发效果验证客户端的合法性socket模块还有一些其他的方法

  8. 初学python之路-day07-字符编码

    今天的博客主要关于字符编码,并对前几天学习的数据类型做些总结. 学习字符编码的目的:解决乱码问题. 应用程序打开文本文件的三步骤  1.打开应用程序  2.将数据加载到内存中  3.cpu将内存中的数 ...

  9. 无向图 解决Unity地图上固定网络上,标记走固定步数能到达的位置

    首先需要了解无向图的定义 参考:https://www.cnblogs.com/wxgblogs/p/5572391.html 我们选择链表的方式进行操作. int StartPositon; int ...

  10. 使用Postman测试https接口时的小问题记录

    测试本地的WebApi接口时,接口是https,自己写的用httpclient测试是可以的, 用postman一直连接不了.原因正是由于https,不过postman在界面上已经给出了可能的原因和解决 ...