通过登入把用户信息和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. shiro教程

    ref https://www.jianshu.com/p/5a35d0100a71 https://www.jianshu.com/p/0366a1675bb6 https://blog.csdn. ...

  2. 记事本:一些js案例以及DOM和BOM

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Appnium-API-Status

    Status Java:// TODO Python:selenium.webdriver.common.utils.is_url_connectable(port) Description Retu ...

  4. hammer.js初探

    hammer.js官方文档 hammerjs是什么 hammerjs是一个短小精悍的库,他可以让我们轻松的实现移动端上的手势. hammerjs的两大优势如下: 为移动端网页添加相关手势. 去除移动端 ...

  5. Java版InfluxDB工具类

    InfluxDB工具类 package com.influxdb.test; import java.util.Map; import org.influxdb.InfluxDB; import or ...

  6. luoguP1131

    时态同步 ...这道题我也不知道咋\(A\)的. 思路: \(anst\) 距离 \(s\) 的最长距离,\(ansp\) 某一节点到祖先所有边的权值和这些些加过的权值和 先求出到\(s\)距离最长的 ...

  7. vue 统一处理token失效问题

    使用http response 拦截器 在main.js中添加 import axios from 'axios'; axios.interceptors.response.use(response ...

  8. python复习基本知识

    print(x or y) """x or y ; x 为 true ,则返回x ,否则返回flase“”“” and 则相反 优先级比较:not > and &g ...

  9. shell判断进程是否存在

    #!/bin/bash #进入rest所有目录,后续在此目录执行动作 cd /data/www/vhosts/go/ #rest的进程数,由于脚本名称为restart_rest.sh,所以排除了对re ...

  10. scrapy+mongodb

    我们都知道scrapy适合爬取大量的网站信息,爬取到的信息储存到数据库显然需要更高的效率,scrapy配合mongodb是非常合适的,这里记录一下如何在scrapy中配置mongodb. 文件结构 $ ...