【问题标题】:Using cookie in asp.net mvc c#在asp.net mvc c#中使用cookie
【发布时间】:2012-02-27 19:35:55
【问题描述】:

我想在我的网站中使用cookie注册几个页面的参数。我尝试了下面的代码,但不喜欢我想要的:

 public ActionResult Index(int? dep, int? cat)
 {
   ......
   string theDept = Request.QueryString["dep"];
   HttpCookie cookie = new HttpCookie("search");
   cookie.Values["dep_name"] = theDept;
   cookie.Expires = DateTime.Now.AddDays(1);
   Response.Cookies.Add(cookie);
   return View();
 }

我在 site.master 中阅读过:

<% 

HttpCookie cookie = Request.Cookies["search"] ;

if ((cookie != null) && (cookie.Value != ""))
{
    Response.Write(cookie.Values["dep_name"].ToString() + "---" +   
    cookie.Values["cat_name"].ToString() + "---" + cookie.Values["brand"].ToString());
}
%>

问题:当我点击Request.QueryString["dep"] 为空的另一个页面时,我显示的cookie 为空。

在我们还没有清除cookie的情况下,如何保存在cookie中而不丢失?

【问题讨论】:

    标签: asp.net asp.net-mvc-2 cookies


    【解决方案1】:

    我以有组织的方式组织了 cookie 获取和插入,以便可以在整个应用程序中使用它。为此,我提出了两种方法SetCookieGetCookie

    您可以简单地将这个类放入您的代码中并进行计算。

    在这里,我将我的类与静态方法放在一起

    public class CookieStore
    {
        public static void SetCookie(string key, string value, TimeSpan expires)
        {
            HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value));
    
            if (HttpContext.Current.Request.Cookies[key] != null)
            {
                var cookieOld = HttpContext.Current.Request.Cookies[key];
                cookieOld.Expires = DateTime.Now.Add(expires);
                cookieOld.Value = encodedCookie.Value;
                HttpContext.Current.Response.Cookies.Add(cookieOld);
            }
            else
            {
                encodedCookie.Expires = DateTime.Now.Add(expires);
                HttpContext.Current.Response.Cookies.Add(encodedCookie);
            }
         }
        public static string GetCookie(string key)
        {
            string value = string.Empty;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
    
            if (cookie != null)
            {
                // For security purpose, we need to encrypt the value.
                HttpCookie decodedCookie = HttpSecureCookie.Decode(cookie);
                value = decodedCookie.Value;
            }
            return value;
        }
    
    }
    

    使用这些,您可以轻松地将值存储在 cookie 中并在需要时获取值

    使用这些方法很简单

    设置 Cookie:

    CookieStore.SetCookie("currency", "GBP", TimeSpan.FromDays(1)); // here 1 is no of days for cookie to live
    

    获取 Cookie:

    string currency= CookieStore.GetCookie("currency");
    

    【讨论】:

    • 保存时,为什么要更新现有的cookie而不是设置一个新的?
    • 我遇到了一个问题,当我在快速手表中看到 cookie 时发现了两次,过期日期为 0001 年 1 月 1 日,所以想更新它。
    • 你在使用来自nuget.org/packages/httpsecurecookie的HttpSecureCookie吗?
    • 不,它是我与机器密钥加密一起使用的自定义类。您可以在此处执行 cookie 保存而无需编码和解码。
    • 我使用这个帮助来使用 HttpSecureCookie codeproject.com/KB/web-security/HttpSecureCookie.aspx。您可以使用相同的
    【解决方案2】:

    我不确定我是否理解这是关于如何正确地将 cookie 发送到客户端的问题,还是您的查询字符串参数存在一些错误。因此,我将发布发送cookie的正确方法,如果我误解了,请随时纠正我。

    无论如何,我相信这一点:

    HttpCookie cookie = new HttpCookie("search");
    

    将重置搜索 cookie

    获取 cookie:

    HttpCookie cookie = HttpContext.Request.Cookies.Get("some_cookie_name");
    

    检查 cookie 的存在:

    HttpContext.Request.Cookies["some_cookie_name"] != null
    

    保存 cookie:

    HttpCookie cookie = new HttpCookie("some_cookie_name");
    HttpContext.Response.Cookies.Remove("some_cookie_name");
    HttpContext.Response.SetCookie(cookie );
    

    【讨论】:

    • 我从事网络工作已经将近六年了,最近我接到了一项任务,其中涉及第一次设置 cookie。奇怪的,狂野的东西。这有帮助,谢谢!
    • 澄清一下,您会将这段代码放在 MVC 项目中的什么位置以避免控制器被垃圾填满?
    • HttpContext.Response.Cookies.Remove 行不是删除 cookie 而不保存它吗?
    猜你喜欢
    • 2013-10-08
    • 2011-07-04
    • 2023-04-11
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    相关资源
    最近更新 更多