如何避免重复的cookie我asp

如何避免重复的cookie我asp

本文介绍了如何避免重复的cookie我asp.net应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是客户端的cookie进行数据(串),这有助于使用每当用户重新打开浏览器相同的数据。
我担心的还有,因为具有相同的名称在相同的URL创建多个Cookie(用户只感觉)
防爆网址: -

I am using a client side cookie to carry a data(string), which is helping to use the same data whenever user reopen the browser.My concern there are multiple cookies where created with the same name on the same url(user feels only)Ex URL:- http://google.com/testapplication

不过,用户在打字不区分大小写,前为波纹管的网址。

But user is typing the url in case-insensitive, ex as bellow.


  1. TestApplication

  2. TESTApplication

有对于用户接口没有差别,但在内部创建不同饼干。可能有人帮助我如何避免这种重复。

There is no difference for the user interface, but internally different cookies were created. Could some one help me how to avoid such duplicates.

谢谢

推荐答案

Cookie名称是区分大小写的一些浏览器如Chrome和Firefox。

Cookie Names are case-sensitive for some browsers such as Chrome and FireFox.

为了避免它,你应该使用一个辅助的方法来创建的cookie,让cookie名称将是整个网站保持一致。

In order to avoid it, you should use a single helper method to create cookie, so that cookie name will be consistent across your site.

例如,

public class CookieHelper
{
    public static void SetCookie(string cookieName, string cookieValue, int days)
    {
        cookieName = cookieName.ToLower();
        var cookie = new HttpCookie(cookieName)
        {
            Value = cookieValue,
            Expires = DateTime.Now.AddDays(days)
        };

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

    public static String GetCookie(string cookieName)
    {
        try
        {
            cookieName = cookieName.ToLower();
            if (HttpContext.Current.Request.Cookies[cookieName] == null)
                return string.Empty;

            return HttpContext.Current.Request.Cookies[cookieName].Value;
        }
        catch
        {
            return string.Empty;
        }
    }
}

这篇关于如何避免重复的cookie我asp.net应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:11