首页 技术 正文
技术 2022年11月8日
0 收藏 953 点赞 1,706 浏览 3099 个字

通过登入把用户信息和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; }
}
}
下一篇: softmax in pytorch
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295