C# 后台代码写入:
public void DeleteCookie(HttpCookie cookie)
{
if (cookie != null)
{
string cookieDomain = this.hiContext.SiteSettings.CookieDomain;
string absoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
Regex regex = new Regex(@"[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)+#34;, RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (regex.IsMatch(cookieDomain) && (absoluteUri.IndexOf(cookieDomain) > -1))
{
cookie.Path = "/";
cookie.Domain = cookieDomain;
}
cookie.Expires = new DateTime(0x777, 10, 12);
this.context.Response.Cookies.Add(cookie);
}
}
public void WriteCookie(HttpCookie cookie, int days, bool autoLogin)
{
if (cookie != null)
{
string cookieDomain = this.hiContext.SiteSettings.CookieDomain;
string absoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
Regex regex = new Regex(@"[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)+#34;, RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (regex.IsMatch(cookieDomain) && (absoluteUri.IndexOf(cookieDomain) > -1))
{
cookie.Path = "/";
cookie.Domain = cookieDomain;
}
if (autoLogin)
{
cookie.Expires = DateTime.Now.AddDays((double) days);
}
this.context.Response.Cookies.Add(cookie);
}
}
闭包调用用户示例:
/******
* localStorage使用
* */
var St = function () {
//写入用户信息
function SetUserInfo(data) {
//获取用户信息
var param = {};
var user = {};
user.userid = data.user.userId;
user.userName = data.user.userName;
user.userEmail = data.user.email;
param.token = data.token;
param.user = user;
window.localStorage.userinfo = JSON.stringify(param);
}
function GetUserInfo() {
if (window.localStorage.userinfo == null) {
Message.Alert("您会话已失效,您将重新登录...");
setTimeout(function () {
window.location.href = root + "index";
}, 2000);
return;
}
var data = JSON.parse(window.localStorage.userinfo);
if (data != undefined && data != null) {
return data.user;
}
}
function GetUserId() {
if (window.localStorage.userinfo == null) {
Message.Alert("您会话已失效,您将重新登录...");
setTimeout(function () {
window.location.href = root + "index";
}, 2000);
return;
}
var data = JSON.parse(window.localStorage.userinfo);
if (data != undefined && data != null) {
return data.user.userid;
}
}
function GetUserName() {
if (window.localStorage.userinfo == null) {
Message.Alert("您会话已失效,您将重新登录...");
setTimeout(function () {
window.location.href = root + "index";
}, 2000);
return;
}
var data = JSON.parse(window.localStorage.userinfo);
if (data != undefined && data != null) {
return data.user.userName;
}
}
function GetUserEmail() {
if (window.localStorage.userinfo == null) {
Message.Alert("您会话已失效,您将重新登录...");
setTimeout(function () {
window.location.href = root + "index";
}, 2000);
return;
}
var data = JSON.parse(window.localStorage.userinfo);
if (data != undefined && data != null) {
return data.user.userEmail;
}
}
function GetToken() {
if (window.localStorage.userinfo == null) {
Message.Alert("您会话已失效,您将重新登录...");
setTimeout(function () {
window.location.href = root + "index";
}, 2000);
return;
}
var data = JSON.parse(window.localStorage.userinfo);
if (data != undefined && data != null) {
return "Bearer " + data.token;
}
}
function Clear() {
window.localStorage.removeItem("userinfo");
}
return {
GetToken: function () { return GetToken(); },
Set: function (data) { SetUserInfo(data); },
GetUserId: function () { return GetUserId(); },
GetUserName: function () { return GetUserName(); },
GetUserEmail: function () { return GetUserEmail(); },
GetUserInfo: function () { return GetUserInfo(); },
Clear: function () { Clear(); }
}
}();