在asp.net中使用cookie mvc c#

时间:2022-09-03 18:40:04

I want to register the parameter of a few pages in my web site using cookie. I tried the below code but not like what I want :

我想使用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();
 }

I read it in site.master :

我在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());
}
%>

Problem: When I click to another page that Request.QueryString["dep"] is null, the cookie that I display is null to.

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

How to store it in the cookie without losing while we not yet clear the cookie?

如果我们尚未清除cookie,如何将其存储在cookie中而不会丢失?

2 个解决方案

#1


57  

I m not sure I understand if this is a question about how to properly send cookies to the client or some bug with your querystring params. So I ll post the proper way of sending cookies and feel free to correct me if I misunderstood.

我不确定我是否理解这是一个关于如何正确地向客户端发送cookie或者你的查询字符串params的一些错误的问题。所以我会发布正确的发送cookie的方式,如果我误解了,请随时纠正我。

In any case though, I believe this:

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

HttpCookie cookie = new HttpCookie("search");

will reset the search cookie

将重置搜索cookie

To get a cookie:

获取cookie:

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

To check for a cookie's existence:

检查cookie的存在:

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

To save a cookie:

要保存cookie:

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

#2


14  

i have organised the cookie fetching and inserting in an organized manner such that it can be used throughout the application. for that purpose i put two methods SetCookie and GetCookie.

我已经组织了cookie以有组织的方式获取和插入,以便可以在整个应用程序中使用它。为此,我把两个方法SetCookie和GetCookie。

You can simply put this class in your code and work out.

您只需将此类放入代码中即可。

Here i put my class with the static methods

在这里,我把我的类与静态方法

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;
    }

}

using these you can easily store values in cookie and fetch value whenever required

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

using these methods is as simple as

使用这些方法就像

For Setting Cookie:

设置Cookie:

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

For Getting Cookie:

对于获取Cookie:

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

#1


57  

I m not sure I understand if this is a question about how to properly send cookies to the client or some bug with your querystring params. So I ll post the proper way of sending cookies and feel free to correct me if I misunderstood.

我不确定我是否理解这是一个关于如何正确地向客户端发送cookie或者你的查询字符串params的一些错误的问题。所以我会发布正确的发送cookie的方式,如果我误解了,请随时纠正我。

In any case though, I believe this:

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

HttpCookie cookie = new HttpCookie("search");

will reset the search cookie

将重置搜索cookie

To get a cookie:

获取cookie:

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

To check for a cookie's existence:

检查cookie的存在:

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

To save a cookie:

要保存cookie:

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

#2


14  

i have organised the cookie fetching and inserting in an organized manner such that it can be used throughout the application. for that purpose i put two methods SetCookie and GetCookie.

我已经组织了cookie以有组织的方式获取和插入,以便可以在整个应用程序中使用它。为此,我把两个方法SetCookie和GetCookie。

You can simply put this class in your code and work out.

您只需将此类放入代码中即可。

Here i put my class with the static methods

在这里,我把我的类与静态方法

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;
    }

}

using these you can easily store values in cookie and fetch value whenever required

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

using these methods is as simple as

使用这些方法就像

For Setting Cookie:

设置Cookie:

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

For Getting Cookie:

对于获取Cookie:

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