Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在ASP.NET中更新cookie_C#_Asp.net Mvc_Model View Controller_Cookies - Fatal编程技术网

C# 无法在ASP.NET中更新cookie

C# 无法在ASP.NET中更新cookie,c#,asp.net-mvc,model-view-controller,cookies,C#,Asp.net Mvc,Model View Controller,Cookies,我为这件事发疯了。我可以写一个cookie,然后再读一遍。但在某个时刻,我想更新它所持有的值。每当我再次得到cookie时,我得到的是初始值,而不是更新后的值。下面是我用于写入/更新和读取cookie的代码 private static HttpCookie WriteCookie(Guid siteId, string siteName) { var cookie = HttpContext.Current.Request.Cookies.Get("UserSe

我为这件事发疯了。我可以写一个cookie,然后再读一遍。但在某个时刻,我想更新它所持有的值。每当我再次得到cookie时,我得到的是初始值,而不是更新后的值。下面是我用于写入/更新和读取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.");
    }
private static HttpCookie WriteCookie(Guid siteId,string siteName)
{
var cookie=HttpContext.Current.Request.Cookies.Get(“用户设置”);
if(cookie!=null){
cookie.Value=EncryptObject(新用户设置模型{SiteID=SiteID,SiteName=SiteName});
HttpContext.Current.Response.Cookies.Set(cookie);
}否则{
cookie=new-HttpCookie(“UserSettings”){Path=“/”,Expires=DateTime.Now.AddDays(1),Value=EncryptObject(new-UserSettingsModel{SiteID=SiteID,SiteName=SiteName});
HttpContext.Current.Response.Cookies.Add(cookie);
}
返回cookie;
}
公共静态用户设置模型GetUserSettings(字符串username=null)
{
var cookie=HttpContext.Current.Request.Cookies.Get(“用户设置”);
if(cookie==null | | string.IsNullOrEmpty(cookie.Value))
{
cookie=ResetUserSettings();
}
var userSettings=解密对象(cookie.Value);
if(userSettings!=null)
{
var siteId=userSettings.siteId;
var siteName=userSettings.siteName;
返回新用户设置模型{SiteID=SiteID,SiteName=SiteName};
}
抛出新的SecurityException(“您没有附加到用户的站点。请与管理员联系。”);
}
GetUserSettings
始终返回最初创建cookie时使用的值。怎么了

编辑:

我尝试直接从控制器中的方法调用WriteCookie。饼干现在已经做好了。我通常通过Ajax请求调用WriteCookie。现在,我真的必须使用JavaScript编写cookie吗,或者我可以用WriteCookie编写cookie吗

谢谢

试着这样做:

var response = HttpContext.Current.Response;
response.Cookies.Remove("UserSettings");
response.Cookies.Add(cookie);
但我怀疑您的实际问题是,您在同一个HTTP请求中调用了
WriteCookie
方法和
GetUserSettings
方法,但这两个方法的工作方式与您可能认为的或预期的不一样

WriteCookie
将cookie写入响应,以便它在后续请求上可用,但是
GetUserSettings
请求读取cookie,因此您将获得启动此请求时最初设置的cookie值,当然是旧价值观。因此,在调用
WriteCookie
更新用户设置cookie的值后,执行重定向,并且仅在后续请求中使用
GetUserSettings
方法


此外,在ASP.NET MVC中,您通常不希望使用静态
HttpContext.Current
对象,而是使用此框架提供给您的抽象。我知道您将这两个方法作为静态方法编写,但是您应该将它们作为
HttpContextBase
对象的扩展方法编写。这样,在HTTP请求的生命周期中,您可以在ASP.NET MVC在许多常见位置提供的抽象基类实例的任何位置调用它们。

是否使用fiddler检查实际发送的内容?我使用的是Chrome浏览器,使用集成开发工具,我可以看到cookie值从未更新过。我想知道为什么要查看请求和响应头以了解发生了什么。据我所知,响应头中没有cookie。我可以在请求头中看到初始cookie值,尽管我尝试了删除然后添加,正如您所建议的那样。那没用。WriteCookie是使用Ajax调用调用的。当此调用返回时,将刷新页面并调用GetUserSettings。因此,这些电话有不同的请求。不要提那些被破坏的Ajax调用,然后再重新加载。我知道这应该被清理。页面是用“window.location.reload();”刷新的,但是我尝试了按Ctrl+F5组合键,仍然得到了旧的值。我意识到else只是第一次被击中。这是它应该工作的方式。如果存在cookie,则应更新cookie。否则它应该被创建。问题是我从Ajax请求调用WriteCookie。我重写了代码,所以cookie现在是通过JavaScript设置的。现在很有魅力。谢谢你指出了正确的方向嗨,安德烈亚斯,谢谢你的评论。是的,这是ajax调用。我简直是在撞我的头。我已经重新编写并删除了对的ajax调用,现在工作得很好。嗨,Andreas,谢谢你的评论。是的,这是ajax调用。我简直是在撞我的头。我重新编写并删除了对var urlSetTheam='@Url.Action(“SetTheam”)'+'?sTheamSelected='+selectedValueText的ajax调用;现在工作很好。