在asp.net中cookie的问题。值的Response.Redirect后恢复 [英] cookie problems in asp.net. Values reverting after response.redirect

查看:91
本文介绍了在asp.net中cookie的问题。值的Response.Redirect后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多SOOOOO小时这是疯了。

I have spent sooooo many hours on this it is insane.

我有一个页面基类,它包含一个的setcookie功能,这基本上是这样的:

I have a page base class which contains a "setcookie" function and this is basically this:

        Dim context As HttpContext = System.Web.HttpContext.Current

        If context.Request.Cookies(cookieName) Is Nothing Then
            Dim cookie As HttpCookie
            cookie.Value = cookieValue
            cookie.Expires = DateTime.Now.AddDays(7)
            context.Response.Cookies.Add(cookie)
        Else
            Dim cookie As HttpCookie = context.Request.Cookies(cookieName)
            cookie.Expires = DateTime.Now.AddDays(7)
            cookie.Value = cookieValue
        End If

此功能是通过一个简单的aspx页面调用。由于这是在测试环境中有我使用cookie的123一previous值。如果我使用调试和监视窗口,我看到的值更改为168成功。

This function is called by a simple aspx page. As this is in a test environment there is a previous value of "123" in the cookie I'm using. If I use the debug and watch window, I see the value change to "168" successfully.

我有一条线是一个调试断点:

I have a debug break point on a line which is:

           Response.Redirect("overview.aspx", False)

在破发点是积极的,在监视窗口中的值是:

When the break point is active, the values in the watch window are:

    currProjectID   168 Integer
    HttpContext.Current.Request.Cookies("currProjectID").Value  "168"   String

(currProjectID在BasePage类,获取属性/设置cookie与上面的功能)

(currProjectID is a property in the basepage class that gets/sets the cookie with the function above)

现在,第二个我走下断点线以上使用F10的变量的变化值!

Now, the second I step off the breakpoint line above using "F10" the values of the variable change!

    HttpContext.Current.Request.Cookies("currProjectID").Value  "123"   String
    currProjectID   123 Integer

这是疯了!在code是条死胡同,调试点立即在Response.Redirect的线以上,但该值已马上改口个所以然来之前!没有什么已经接近的setcookie例行所以请请请会有人救我的精神错乱,告诉我什么错误!?

This is insane! The code goes nowhere, the debug point is immediately under the "response.redirect" line above and yet the values have immediately changed to what they were before! Nothing has gone near the "setcookie" routine so please please please would someone save my insanity and tell me what is going wrong!?

推荐答案

您必须:
- 获得请求的cookie
- 更新饼干
- 响应发送的cookie

You have to: - get the cookie from request - update cookie - send cookie in response

如果您在响应不发送的cookie,浏览器就不会知道这个改变任何东西!
Cookies是不是聪明的自我更新。

If you dont sent cookie in the response, browser wouldn't know anything about the change !!! Cookies aren't that clever to update themselves.

希望它帮助。

更新

var cookieDetails = Request.Cookies["yourCookie"];
if (cookieDetails != null)
{
    cookieDetails.Values["someValue"] = valueToAssign;
}
else
{
    cookieDetails = new HttpCookie("yourCookie");
    cookieDetails.Values.Add("someValue", valueToAssign);
}
Response.Cookies.Add(cookieDetails);

本例中设置一个cookie。正如你可以看到,如果cookie存在,第二只是创建新的cookie的第一位检查。

this example sets a cookie. as you can see the first bit is checking if cookie exists and the second just creates new cookie.

你缺少这会将cookie发送回浏览器的最后一位。

you are missing the last bit which sends the cookie back to browser

Response.Cookies.Add(cookieDetails);

希望它帮助。

这篇关于在asp.net中cookie的问题。值的Response.Redirect后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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