无法更新ASP.NET饼干 [英] Unable to update cookie in ASP.NET

查看:91
本文介绍了无法更新ASP.NET饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在这个坚果。我可以写一个cookie,然后再读一遍。但在某些时候,我想更新其持有的价值。每当我再次拿到饼干,我得到的初始值,而非更新的。下面是code I用于写入/更新和cookie的阅读。

I'm going nuts over this. I can write to a cookie, and then read it again. But at some point, i want to update the value it holds. Whenever i get the cookie again, i get the initial value, not the updated one. Below is the code i use for write/update and read of the cookie.

    private static HttpCookie WriteCookie(Guid siteId, string siteName)
    {
        var cookie = HttpContext.Current.Request.Cookies.Get("UserSettings");
        if(cookie != null) {
            cookie.Value = EncryptObject(new UserSettingsModel { SiteID = siteId, SiteName = siteName });
            HttpContext.Current.Response.Cookies.Set(cookie);
        }else {
            cookie = new HttpCookie("UserSettings") { Path = "/", Expires = DateTime.Now.AddDays(1), Value = EncryptObject(new UserSettingsModel { SiteID = siteId, SiteName = siteName }) };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        return cookie;
    }

    public static UserSettingsModel GetUserSettings(string username = null)
    {
        var cookie = HttpContext.Current.Request.Cookies.Get("UserSettings");
        if (cookie == null || string.IsNullOrEmpty(cookie.Value))
        {
            cookie = ResetUserSettings();
        }
        var userSettings = DecryptObject<UserSettingsModel>(cookie.Value);
        if (userSettings != null)
        {
            var siteId = userSettings.SiteID;
            var siteName = userSettings.SiteName;
            return new UserSettingsModel { SiteID = siteId, SiteName = siteName };
        }
        throw new SecurityException("You have no site attached to your user. Contact an administrtor.");
    }

GetUserSettings 总是返回该Cookie最初与创造的价值。怎么了?

GetUserSettings always returns the value that the cookie was initially created with. What's wrong?

编辑:

我试图直接从控制器的方法调用WriteCookie。该cookie被设置完毕。我通常称为通过Ajax请求WriteCookie。现在,我真的有写使用JavaScript的cookie,或者我可以采用某种只是使用WriteCookie呢?

I tried calling WriteCookie directly from a method in a Controller. The cookie was now set. I usually called WriteCookie via an Ajax request. Now, do i really have to write the cookie using JavaScript, or can i somehow just do it using WriteCookie?

谢谢!

推荐答案

尝试这样的:

var response = HttpContext.Current.Response;
response.Cookies.Remove("UserSettings");
response.Cookies.Add(cookie);

不过,我怀疑你的实际的问题是,你是调用 WriteCookie 方法,并在该 GetUserSettings 方法像你想象的那样,或如你所期望它不工作一样HTTP请求。

But I suspect that your actual problem is that you are calling the WriteCookie method and the GetUserSettings method in the same HTTP request which doesn't work as you might think it would or as you might expect it to.

WriteCookie 写入cookie来的响应,以便它可以在后续请求 GetUserSettings 读取来自请求该Cookie 让你得到时,该请求被发起,它当然是旧值,最初设置cookie的值。因此调用后 WriteCookie 更新用户设置cookie的值,执行重定向,只有在后续请求中使用 GetUserSettings 方法。

The WriteCookie writes the cookie to Response so that it is available on subsequent requests but the GetUserSettings reads the cookie from the Request so you are getting the value of the cookie that was initially set when this request was initiated which of course is the old value. So after calling the WriteCookie to update the value of the user settings cookie, perform a redirect and only on the subsequent request use the GetUserSettings method.

此外,在ASP.NET MVC您通常不希望使用静态 HttpContext.Current 对象,但是使用抽象,这个框架提供给你。我知道你写了这2种方法为静态,但你应该写他们作为一个扩展方法的 HttpContextBase 对象实例。这样,您就可以在任何地方都称他们在那里你一个HTTP请求的一生中有这个抽象基类,ASP.NET MVC为您提供了很多常见的地方的一个实例。

Also in ASP.NET MVC you typically don't want to use the static HttpContext.Current object but use the abstractions that this framework provides to you. I know you wrote those 2 methods as static but you should have written them as an extension method to the HttpContextBase object for example. This way you could have called them anywhere where you had an instance of this abstract base class which ASP.NET MVC provides you in many common places during the lifetime of an HTTP request.

这篇关于无法更新ASP.NET饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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