C# 使用cookie实现ITempDataProvider vs
我试图使用TempData将数据从一个请求发送到另一个请求 但是,由于TempData使用服务器会话,并且该应用程序将被网络化,因此我必须使用cookie或数据库持久性,因为会话不会从一台服务器传输到另一台服务器 有许多实现可用于使用cookie而不是默认会话:C# 使用cookie实现ITempDataProvider vs,c#,asp.net-mvc,cookies,tempdata,C#,Asp.net Mvc,Cookies,Tempdata,我试图使用TempData将数据从一个请求发送到另一个请求 但是,由于TempData使用服务器会话,并且该应用程序将被网络化,因此我必须使用cookie或数据库持久性,因为会话不会从一台服务器传输到另一台服务器 有许多实现可用于使用cookie而不是默认会话: public class CookieTempDataProvider : ITempDataProvider { const string CookieName = "TempData"; public void S
public class CookieTempDataProvider : ITempDataProvider
{
const string CookieName = "TempData";
public void SaveTempData(
ControllerContext controllerContext,
IDictionary<string, object> values)
{
// convert the temp data dictionary into json
string value = Serialize(values);
// compress the json (it really helps)
var bytes = Compress(value);
// sign and encrypt the data via the asp.net machine key
value = Protect(bytes);
// issue the cookie
IssueCookie(controllerContext, value);
}
public IDictionary<string, object> LoadTempData(
ControllerContext controllerContext)
{
// get the cookie
var value = GetCookieValue(controllerContext);
// verify and decrypt the value via the asp.net machine key
var bytes = Unprotect(value);
// decompress to json
value = Decompress(bytes);
// convert the json back to a dictionary
return Deserialize(value);
}
...
我在网上找到的任何解决方案都没有使cookie过期;因此,基本上,它们不是真正的临时数据。它们都只是让cookie通过LoadTempData()保存下来,因此在这一点上,您甚至可以根本不使用TempData 在下面的实现中,cookie将只在HTTP请求的持续时间内有效(正如TempData所假定的那样),如果您想保持更长时间,可以像通常一样使用
TempData.keep(“yourKey”)
,它将再次调用SaveTempData()方法(如所示)
最后一件事,这段代码没有针对速度或安全性进行优化
public class CookieTempDataProvider : ITempDataProvider
{
public const string TempDataCookieKey = "__ControllerTempData";
public IDictionary<string, object> LoadTempData(ControllerContext controller)
{
HttpCookie cookie = controller.HttpContext.Request.Cookies[TempDataCookieKey];
Dictionary<string, object> tempDataDictionary = new Dictionary<string, object>();
if (cookie != null)
{
for (int keyIndex = 0; keyIndex < cookie.Values.Count; keyIndex++)
{
string key = cookie.Values.GetKey(keyIndex);
if (!string.IsNullOrEmpty(key))
{
string base64Value = cookie.Values.Get(keyIndex);
byte[] buffer = Convert.FromBase64String(base64Value);
using (MemoryStream ms = new MemoryStream(buffer))
{
BinaryFormatter formatter = new BinaryFormatter();
object value = formatter.Deserialize(ms);
tempDataDictionary.Add(key, value);
}
}
}
cookie.Expires = DateTime.Now.AddDays(-1d); // expire cookie so no other request gets it
controller.HttpContext.Response.SetCookie(cookie);
}
return tempDataDictionary;
}
public void SaveTempData(ControllerContext controller, IDictionary<string, object> values)
{
HttpCookie cookie = controller.HttpContext.Request.Cookies[TempDataCookieKey];
bool hasValues = (values != null && values.Count > 0);
if (cookie == null)
{
cookie = new HttpCookie(TempDataCookieKey);
controller.HttpContext.Response.Cookies.Add(cookie);
}
if (hasValues)
{
foreach (KeyValuePair<string, object> kvp in values)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, kvp.Value);
byte[] bytes = ms.GetBuffer();
string base64Value = Convert.ToBase64String(bytes);
string keyExists = cookie.Values.Get(kvp.Key);
if (keyExists != null)
{
cookie.Values.Set(kvp.Key, base64Value);
}
else
{
cookie.Values.Add(kvp.Key, base64Value);
}
}
}
cookie.Expires = DateTime.Now.AddDays(1d);
controller.HttpContext.Response.SetCookie(cookie);
}
else
{
// delete session if null values are passed
if (controller.HttpContext.Request.Cookies[TempDataCookieKey] != null)
{
cookie.Expires = DateTime.Now.AddDays(-1d); // expire cookie so no other request gets it
}
}
}
}
公共类CookieTempDataProvider:ITempDataProvider
{
public const字符串TempDataCookieKey=“\uuu ControllerTempData”;
公共IDictionary LoadTempData(控制器上下文控制器)
{
HttpCookie cookie=controller.HttpContext.Request.Cookies[TempDataCookieKey];
Dictionary tempDataDictionary=新字典();
if(cookie!=null)
{
对于(int-keyIndex=0;keyIndex0);
if(cookie==null)
{
cookie=新的HttpCookie(TempDataCookieKey);
controller.HttpContext.Response.Cookies.Add(cookie);
}
if(hasValues)
{
foreach(值中的KeyValuePair kvp)
{
BinaryFormatter formatter=新的BinaryFormatter();
使用(MemoryStream ms=new MemoryStream())
{
序列化(ms,kvp.Value);
byte[]bytes=ms.GetBuffer();
string base64Value=Convert.ToBase64String(字节);
字符串keyExists=cookie.Values.Get(kvp.Key);
如果(keyExists!=null)
{
cookie.Values.Set(kvp.Key,base64Value);
}
其他的
{
cookie.Values.Add(kvp.Key,base64Value);
}
}
}
cookie.Expires=DateTime.Now.AddDays(1d);
controller.HttpContext.Response.SetCookie(cookie);
}
其他的
{
//如果传递空值,则删除会话
if(controller.HttpContext.Request.Cookies[TempDataCookieKey]!=null)
{
cookie.Expires=DateTime.Now.AddDays(-1d);//使cookie过期,以便没有其他请求获得它
}
}
}
}
在您的实现中删除cookie有什么问题?我有点好奇,为什么我发现的其他实现都没有删除cookie。我希望cookie被删除,就像微软删除会话一样;然而,这不是我在上面引用的Brock Allen代码中发现的。
public class CookieTempDataProvider : ITempDataProvider
{
public const string TempDataCookieKey = "__ControllerTempData";
public IDictionary<string, object> LoadTempData(ControllerContext controller)
{
HttpCookie cookie = controller.HttpContext.Request.Cookies[TempDataCookieKey];
Dictionary<string, object> tempDataDictionary = new Dictionary<string, object>();
if (cookie != null)
{
for (int keyIndex = 0; keyIndex < cookie.Values.Count; keyIndex++)
{
string key = cookie.Values.GetKey(keyIndex);
if (!string.IsNullOrEmpty(key))
{
string base64Value = cookie.Values.Get(keyIndex);
byte[] buffer = Convert.FromBase64String(base64Value);
using (MemoryStream ms = new MemoryStream(buffer))
{
BinaryFormatter formatter = new BinaryFormatter();
object value = formatter.Deserialize(ms);
tempDataDictionary.Add(key, value);
}
}
}
cookie.Expires = DateTime.Now.AddDays(-1d); // expire cookie so no other request gets it
controller.HttpContext.Response.SetCookie(cookie);
}
return tempDataDictionary;
}
public void SaveTempData(ControllerContext controller, IDictionary<string, object> values)
{
HttpCookie cookie = controller.HttpContext.Request.Cookies[TempDataCookieKey];
bool hasValues = (values != null && values.Count > 0);
if (cookie == null)
{
cookie = new HttpCookie(TempDataCookieKey);
controller.HttpContext.Response.Cookies.Add(cookie);
}
if (hasValues)
{
foreach (KeyValuePair<string, object> kvp in values)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, kvp.Value);
byte[] bytes = ms.GetBuffer();
string base64Value = Convert.ToBase64String(bytes);
string keyExists = cookie.Values.Get(kvp.Key);
if (keyExists != null)
{
cookie.Values.Set(kvp.Key, base64Value);
}
else
{
cookie.Values.Add(kvp.Key, base64Value);
}
}
}
cookie.Expires = DateTime.Now.AddDays(1d);
controller.HttpContext.Response.SetCookie(cookie);
}
else
{
// delete session if null values are passed
if (controller.HttpContext.Request.Cookies[TempDataCookieKey] != null)
{
cookie.Expires = DateTime.Now.AddDays(-1d); // expire cookie so no other request gets it
}
}
}
}