ASP。NET MVC Cookie在页面更改时失去了价值

时间:2022-12-02 14:03:59

When a user logs in to my site, I create a cookie with some info in it. However, whenever they change page from logging in, the cookie loses it's value. Cookie is still there but it's empty.

当用户登录到我的站点时,我创建一个包含一些信息的cookie。但是,无论何时它们改变了登录页面,cookie就失去了它的价值。Cookie仍然在那里,但它是空的。

I've checked my code and the cookie doesn't get rewritten by anything I've done. Does anyone have any idea to why the cookie becomes empty when the page is changed?

我检查了我的代码,cookie没有被我做过的任何事情重写。有人知道为什么当页面改变时,cookie会变成空的吗?

Here's the method for creating the cookie.

这是创建cookie的方法。

public static void CreateUserCookie(long userId, string username, bool rememberMe) {

        HttpCookie cookie = new HttpCookie("CookieName");
        cookie.Value = string.Format("{0}+{1}+{2}", userId, username, SecurityUtils.CreateHashedCookieValue(userId, username));

        if (rememberMe) {
            cookie.Expires = DateTime.Now.AddMonths(1);
        } else {
            cookie.Expires = DateTime.MinValue;
        }

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

1 个解决方案

#1


2  

When you call this method, do you pass in true for the "rememberMe" parameter? If not, the cookie will instantly expire.

当您调用这个方法时,您是否为“remember”参数传递了true ?否则,cookie将立即过期。

You haven't shown your calling code, so this is potentially what you've done.

您还没有显示您的调用代码,所以这可能是您所做的。

#1


2  

When you call this method, do you pass in true for the "rememberMe" parameter? If not, the cookie will instantly expire.

当您调用这个方法时,您是否为“remember”参数传递了true ?否则,cookie将立即过期。

You haven't shown your calling code, so this is potentially what you've done.

您还没有显示您的调用代码,所以这可能是您所做的。