using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Newtonsoft.Json;
namespace StaffMsg
{
public class CommHelp
{
//创建票据
public static void GetTick(Models.LoginAdmin login)
{
string usedate = JsonConvert.SerializeObject(login);
//创建票据,设置其生命周期,存入数据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,"logindate",DateTime.Now,DateTime.Now.AddDays(1),false,usedate);
//加密票据
string stick = FormsAuthentication.Encrypt(ticket);
//存入cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,stick);
//删除已存在的票据信息
HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
//上传当前生成的cookie信息
HttpContext.Current.Response.Cookies.Add(cookie);
}
//票据取值
public static Models.LoginAdmin GetDate()
{
//通过cookie获取当前存在的票据的value
string tick = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]?.Value;
//对其进行空值判断并返回
if (string.IsNullOrWhiteSpace(tick))
{
return default;
}
//将获取到的票据value进行编译
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(tick);
//通过票据获取到数据
string date = ticket.UserData;
//转化存入数据模型
Models.LoginAdmin model = JsonConvert.DeserializeObject(date);
return model;
}
}
}