6

有没有办法让 TempData 存储在浏览器的 cookie 中,而不是 Session State。我在我的网站上禁用了会话状态。

谢谢。

4

4 回答 4

6

您可以使用 brock Allen 的 Cookie TempData 提供程序。在这里有完整的文档,它也可以作为 NuGet 包提供

除其他事项外,它还考虑了一个重要问题:安全性。

让 MVC TempData 使用这个包真的很容易。

于 2013-03-19T21:05:14.553 回答
3

您可以指定自己的自定义 TempDataProvider 并将其写入临时数据中。

查看此博客文章,了解某人使用自定义临时数据提供程序的示例

于 2010-09-26T23:40:26.077 回答
1

我使用以下小类文件:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.Mvc;

/* 16-09-2010
 * pulled from Microsoft.Web.Mvc Futures
 * be careful in case future versions of the mvc dll incorporate this
 * 
 */

namespace yournamespace
{
    public class CookieTempDataProvider : ITempDataProvider
    {
        internal const string TempDataCookieKey = "__ControllerTempData";
        readonly 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);

                cookie.Expires = DateTime.MinValue;
                cookie.Value = string.Empty;

                if (_httpContext.Response != null && _httpContext.Response.Cookies != null)
                {
                    HttpCookie responseCookie = _httpContext.Response.Cookies[TempDataCookieKey];
                    if (responseCookie != null)
                    {
                        cookie.Expires = DateTime.MinValue;
                        cookie.Value = string.Empty;
                    }
                }

                return deserializedTempData;
            }

            return new Dictionary<string, object>();
        }

        protected virtual void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
        {
            var cookieValue = SerializeToBase64EncodedString(values);

            var cookie = new HttpCookie(TempDataCookieKey)
                             {
                                 HttpOnly = true, Value = cookieValue
                             };

            _httpContext.Response.Cookies.Add(cookie);
        }

        public static IDictionary<string, object> DeserializeTempData(string base64EncodedSerializedTempData)
        {
            var bytes = Convert.FromBase64String(base64EncodedSerializedTempData);
            var memStream = new MemoryStream(bytes);
            var binFormatter = new BinaryFormatter();
            return binFormatter.Deserialize(memStream, null) as IDictionary<string, object>;
        }

        public static string SerializeToBase64EncodedString(IDictionary<string, object> values)
        {
            var memStream = new MemoryStream();
            memStream.Seek(0, SeekOrigin.Begin);
            var binFormatter = new BinaryFormatter();
            binFormatter.Serialize(memStream, values);
            memStream.Seek(0, SeekOrigin.Begin);
            var 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);
        }
    }
}

然后将其添加到我的控制器中:

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);
        TempDataProvider = new CookieTempDataProvider(requestContext.HttpContext); 
    }

似乎工作正常...

于 2010-09-27T10:18:56.987 回答
-1

纳扎夫,

试试这个来删除你的cookies:

public void DeleteCookie(string name)
{
    DateTime now = DateTime.UtcNow;
    string cookieKey = name;
    var cookie = new HttpCookie(cookieKey, null)
    {
        Expires = now.AddDays(-1)
    };
    HttpContext.Response.Cookies.Set(cookie);
}

用法:

DeleteCookie("__ControllerTempData");
于 2010-09-29T07:26:36.673 回答