using System;
|
using System.Web;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
|
namespace BatchService.Framework.Utility
|
{
|
/// <summary>
|
/// Cookie帮助类
|
/// </summary>
|
public class Cookie
|
{
|
/// <summary>
|
/// 取Cookie
|
/// </summary>
|
/// <param name="name"></param>
|
/// <returns></returns>
|
public static HttpCookie Get(string name)
|
{
|
return HttpContext.Current.Request.Cookies[name];
|
}
|
|
/// <summary>
|
/// 取Cookie值
|
/// </summary>
|
/// <param name="name"></param>
|
/// <returns></returns>
|
public static string GetValue(string name)
|
{
|
var httpCookie = Get(name);
|
if (httpCookie != null)
|
return httpCookie.Value;
|
else
|
return string.Empty;
|
}
|
|
/// <summary>
|
/// 移除Cookie
|
/// </summary>
|
/// <param name="name"></param>
|
public static void Remove(string name)
|
{
|
Cookie.Remove(Cookie.Get(name));
|
}
|
|
public static void Remove(HttpCookie cookie)
|
{
|
if (cookie != null)
|
{
|
cookie.Expires = DateTime.Now;
|
Cookie.Save(cookie);
|
}
|
}
|
|
/// <summary>
|
/// 保存Cookie
|
/// </summary>
|
/// <param name="name"></param>
|
/// <param name="value"></param>
|
/// <param name="expiresHours"></param>
|
public static void Save(string name, string value, int expiresHours = 0)
|
{
|
var httpCookie = Get(name);
|
if (httpCookie == null)
|
httpCookie = Set(name);
|
|
httpCookie.Value = value;
|
Cookie.Save(httpCookie, expiresHours);
|
}
|
|
|
public static void Save(HttpCookie cookie, int expiresHours = 0)
|
{
|
//string domain = Fetch.ServerDomain;
|
//string urlHost = HttpContext.Current.Request.Url.Host.ToLower();
|
//if (domain != urlHost)
|
// cookie.Domain = domain;
|
|
//if (expiresHours > 0)
|
// cookie.Expires = DateTime.Now.AddMinutes(expiresHours);
|
|
//HttpContext.Current.Response.Cookies.Add(cookie);
|
Save(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, cookie, expiresHours * 60);
|
}
|
|
public static void Save(HttpContextBase context, HttpCookie cookie, int expiresMinutes = 0)
|
{
|
string domain = Fetch.ServerDomain;
|
string urlHost = context.Request.Url.Host.ToLower();
|
if (domain != urlHost)
|
cookie.Domain = domain;
|
|
if (expiresMinutes > 0)
|
cookie.Expires = DateTime.Now.AddMinutes(expiresMinutes);
|
|
context.Response.Cookies.Add(cookie);
|
}
|
|
public static HttpCookie Set(string name)
|
{
|
return new HttpCookie(name);
|
}
|
|
private static Dictionary<string, string> sessionIdMap = new Dictionary<string, string>();
|
private static Dictionary<string, string> loginFailsMap = new Dictionary<string, string>();
|
|
|
public static bool isValidUser(string id, bool isLogin=false)
|
{
|
string sessionId = HttpContext.Current.Session.SessionID;
|
if (isLogin)
|
{
|
sessionIdMap.Add(sessionId, id);
|
return true;
|
}
|
string historySessionId = sessionIdMap.ContainsKey(sessionId) ? sessionIdMap[sessionId] : "";
|
if (id!=historySessionId)
|
{
|
return false;
|
}
|
return true;
|
}
|
|
}
|
}
|