什么是更新这是在ASP.NET中的previous要求设置cookie的最佳做法是什么? [英] What is the best practice for updating a cookie that was set on a previous request in ASP.NET?

查看:134
本文介绍了什么是更新这是在ASP.NET中的previous要求设置cookie的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是该方案。与关键字的cookie 的myCookie已定于previous请求。我可以通过 HttpContext.Request.Cookies.Get(myCookie时)访问它。我想进行更新,如增加了另一个值Cookie值集合,但我不是100%肯定,我这样做是正确的。

我在下面的示例中正确地这样做呢?

 公共静态无效UpdateCookie(HttpContext的背景下,串cookieName,动作<&的HttpCookie GT; updateCookie){
        VAR饼干= context.Request.Cookies.Get(cookieName);
        updateCookie(饼干);
        context.Response.Cookies.Set(饼干);
   }


解决方案

要更新一个cookie,你只需要重新设置Cookie使用新值。请注意,您必须包括所有要保留的数据,作为新的cookie将取代previously设置cookie的。我会假设你updateCookie()的实现了这一点。

否则,您一般premise是正确的。下面是我用很多次做到这一点的实现。 (注:_page是对当前页面的引用):

  ///<总结>
///从现在更新饼干,以期满时间的给定时间量。
///< /总结>
公共无效UpdateCookie(列表< KeyValuePair<字符串,字符串>> cookieItems,时间跨度cookieLife)
{
    的HttpCookie饼干= _page.Request.Cookies [COOKIE_NAME]?新的HttpCookie(COOKIE_NAME);    的foreach(KeyValuePair<字符串,字符串> cookieItem在cookieItems)
    {
        cookie.Values​​ [cookieItem.Key] = cookieItem.Value;
    }    如果(cookieLife.HasValue)
    {
        cookie.Expires = DateTime.Now.Add(cookieLife.Value);
    }
    _page.Response.Cookies.Set(饼干);
}

Here is the scenario. A cookie with the key "MyCookie" has been set on a previous request. I can access it via HttpContext.Request.Cookies.Get("MyCookie"). I want to perform an update such as adding another value to the Cookie Values collection, but I'm not 100% sure I am doing it right.

Am I doing this correctly in the following example?

   public static void UpdateCookie(HttpContext context, string cookieName, Action<HttpCookie> updateCookie){
        var cookie = context.Request.Cookies.Get(cookieName);
        updateCookie(cookie);
        context.Response.Cookies.Set(cookie);
   }

解决方案

To update a cookie, you need only to set the cookie again using the new values. Note that you must include all of the data you want to retain, as the new cookie will replace the previously set cookie. I'm going to assume that your implementation of updateCookie() does just that.

Otherwise, your general premise is correct. Here's an implementation I've used many times to do just that. (Note: _page is a reference to the current Page):

/// <summary> 
/// Update the cookie, with expiration time a given amount of time from now.
/// </summary>
public void UpdateCookie(List<KeyValuePair<string, string>> cookieItems, TimeSpan? cookieLife)
{
    HttpCookie cookie = _page.Request.Cookies[COOKIE_NAME] ?? new HttpCookie(COOKIE_NAME);

    foreach (KeyValuePair<string, string> cookieItem in cookieItems)
    {
        cookie.Values[cookieItem.Key] = cookieItem.Value;
    }

    if (cookieLife.HasValue)
    {
        cookie.Expires = DateTime.Now.Add(cookieLife.Value);
    } 
    _page.Response.Cookies.Set(cookie);
}

这篇关于什么是更新这是在ASP.NET中的previous要求设置cookie的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆