Tôi đang cố gắng sử dụng nhà cung cấp tùy chỉnh ITempDataProvider để lưu trữ TempData trong cookie của trình duyệt thay vì trạng thái phiên. Tuy nhiên, mọi thứ hoạt động tốt ngoại trừ việc tôi không thể xóa cookie khỏi luồng Trả lời sau khi đọc.ASP.NET MVC TempData trong cookie trình duyệt
Bất kỳ ý tưởng nào?
Cảm ơn!
public class CookieTempDataProvider : ITempDataProvider
{
internal const string TempDataCookieKey = "__ControllerTempData";
HttpContextBase _httpContext;
public CookieTempDataProvider(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
_httpContext = httpContext;
}
public HttpContextBase HttpContext
{
get
{
return _httpContext;
}
}
protected virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
{
HttpCookie cookie = _httpContext.Request.Cookies[TempDataCookieKey];
if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
{
IDictionary<string, object> deserializedTempData = DeserializeTempData(cookie.Value);
// Remove cookie
cookie.Expires = DateTime.MinValue;
cookie.Value = string.Empty;
_httpContext.Request.Cookies.Remove(TempDataCookieKey);
if (_httpContext.Response != null && _httpContext.Response.Cookies != null)
{
HttpCookie responseCookie = _httpContext.Response.Cookies[TempDataCookieKey];
if (responseCookie != null)
{
// Remove cookie
cookie.Expires = DateTime.MinValue;
cookie.Value = string.Empty;
_httpContext.Response.Cookies.Remove(TempDataCookieKey);
}
}
return deserializedTempData;
}
return new Dictionary<string, object>();
}
protected virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
{
string cookieValue = SerializeToBase64EncodedString(values);
var cookie = new HttpCookie(TempDataCookieKey);
cookie.HttpOnly = true;
cookie.Value = cookieValue;
_httpContext.Response.Cookies.Add(cookie);
}
public static IDictionary<string, object> DeserializeTempData(string base64EncodedSerializedTempData)
{
byte[] bytes = Convert.FromBase64String(base64EncodedSerializedTempData);
var memStream = new MemoryStream(bytes);
var binFormatter = new BinaryFormatter();
return binFormatter.Deserialize(memStream, null) as IDictionary<string, object> /*TempDataDictionary : This returns NULL*/;
}
public static string SerializeToBase64EncodedString(IDictionary<string, object> values)
{
MemoryStream memStream = new MemoryStream();
memStream.Seek(0, SeekOrigin.Begin);
var binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, values);
memStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = memStream.ToArray();
return Convert.ToBase64String(bytes);
}
IDictionary<string, object> ITempDataProvider.LoadTempData(ControllerContext controllerContext)
{
return LoadTempData(controllerContext);
}
void ITempDataProvider.SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
{
SaveTempData(controllerContext, values);
}
}
Hãy thực sự cẩn thận những gì bạn lưu trữ trong cookie của khách hàng. Nói chung đó là một ý tưởng tồi để giữ nội dung ở đó. Không cố gắng phán xét, nhưng nó đang làm những thứ như vậy khiến cho [DotNetNuke đau khổ] (http://www.youtube.com/watch?v=yghiC_U2RaM) từ [khai thác lỗ hổng] gần đây (http: // weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx). –
Đồng ý với @cottsak. Ngoài ra, nó được gửi cùng với * mọi yêu cầu. * Mọi hình ảnh. Mỗi tập lệnh. Mỗi ... –
@cottsak và @Craig: Tôi đang cố gắng chỉ lưu trữ các thông báo hiển thị như "tin nhắn của bạn đã được gửi". Tôi không lưu trữ bất kỳ dữ liệu nhạy cảm nào trong đó. –