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-]+)+$", 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-]+)+$", 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(); } }}();
相关内容
RootDataNotcoin代币NOT
数据某巨鲸从币安转出1355万枚LDO约
沉寂3年的SHIB地址24H内从Coin
RootData29999枚ETH从未知
某鲸鱼30分钟前从Binance转出13
某巨鲸11小时前从OKX提出2410枚M
RootData299999枚ETH从币
RootData39773枚ETH从未知
ChainCatcher将携手RootData在胡志明市开展“Web3未来之夜”活动
RootData:Avail代币(AVAIL)24小时涨幅达36.44%
文章来源:
财源网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 931614094@qq.com 举报,一经查实,本站将立刻删除。