Why aren't my cookie settings applied?

Based on the answer here: How can I write and read bool values ​​to / from cookies? I am trying to implement a cookie read / write code.

I have a number of checkboxes on the page whose checked state should be saved in / in the form of cookies; when the [re] page loads, the last state should tell the flags whether they should be checked or not. But that does not work. Here are the relevant sections of the page:

@{ bool twitterSelected = false; . . . var selectTwitterCookie = Request.Cookies["selectTwitter"]; . . . if (selectTwitterCookie != null) { twitterSelected = Convert.ToBoolean(selectTwitterCookie); } . . . if (IsPost) { Response.Cookies["selectTwitter"].Value = twitterSelected.ToString(); Response.Cookies["selectTwitter"].Expires = DateTime.Now.AddYears(1); . . . } 

}

 <section class="featured"> <div class="content-wrapper"> <h1>Stuff!!!</h1> <form method="POST"> <fieldset> <legend>Opt In or Out</legend> <input type="checkbox" id="selectTwitter" name="selectTwitter" checked=@twitterSelected >I'm Twitterpated!</input><br /> <input type="submit" id="btnSubmit" name="btnSubmit" value="Save Config changes"</input> </fieldset> </form> </div> </section> 

However, not only the checkbox is not restored to the state that I saved after publishing, when navigating, and then back to the page, I get: "System.InvalidCastException is not handled by the user code HResult = -2147467262 Message = Unable to cast an object of type" System.Web .HttpCookie "to enter" System.IConvertible ". Source = mscorlib Stack Trace: in System.Convert.ToBoolean (object value) in ASP._Page_About_cshtml.Execute () in c: \ DuckbilledPlatypi \ About.cshtml: line 27 ,, InnerException : "

And here is the YSOD:

Server error in application "/". Cannot apply an object of type "System.Web.HttpCookie" to type "System.IConvertible". Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and its occurrence in the code. Exception Details: System.InvalidCastException: Cannot reset an object of type "System.Web.HttpCookie" to enter "System.IConvertible". Source Error: Line 25: //bool.TryParse (selectTwitterCookie, out twitterSelected); Line 26: // Alternatively, you can use: Line 27: twitterSelected = Convert.ToBoolean (selectTwitterCookie); Line 28:} Line 29: if (selectBingCookie! = Null) Source file: c: \ DuckbilledPlatypi \ About.cshtml Line: 27 Stack traces: [InvalidCastException: it is impossible to cast an object of type "System.Web.HttpCookie" to enter "System. IConvertible ".] System.Convert.ToBoolean (object value) +18 ASP._Page_About_cshtml.Execute () in c: \ DuckbilledPlatypi \ About.cshtml: 27 System.Web.WebPages.WebPageBase.ExecutePageHierarchy () +197 System.WebPageHierarchy () +197 System. WebPages.WebPage.ExecutePageHierarchy (IEnumerable`1 executors) +69 System.Web.WebPages.WebPage.ExecutePageHierarchy () +151 System.Web.WebPages.WebPageBase.ExecutePageHierarchy (WebPageContextContext page, writer + Writer Text Web.WebPages.WebPageHttpHandler.ProcessRequestInternal (HttpContextBase httpContext) +114 Version Information: Microsoft.NET Framework Version: 4.0.30319; ASP.NET Version: 4.0.30319.18033

So, how can I successfully save and restore the state of the checkboxes?

UPDATE

changing this:

 twitterSelected = Convert.ToBoolean(selectTwitterCookie); 

... to that:

 twitterSelected = Convert.ToBoolean(selectTwitterCookie.Value); 

... got rid of err msg, but still does not restore the value (checked state) that I save.

UPDATE 2

Oddly enough, two checkboxes now work (the two that I checked remain checked). Why others are not mysterious so far - the code for them all is identical, or at least it seems so until now. I have to dig deeper ...

UPDATE 3

They only worked, because there was a mismatch in the line used to install them, and another to restore them. Now I changed the default value of bools from false to true, and now all the checkboxes are always checked (after publishing / adding [re] page loads). So, I still don't know why the following does not work:

 bool twitterSelected = true; var selectTwitterCookie = Request.Cookies["selectTwitter"]; if (selectTwitterCookie != null) { twitterSelected = Convert.ToBoolean(selectTwitterCookie.Value); } . . . if (IsPost) { Response.Cookies["selectTwitter"].Value = twitterSelected.ToString(); <input type="checkbox" id="selectTwitter" name="selectTwitter" checked=@twitterSelected >I'm Twitterpated!</input><br /> 

These permutations also do not work:

 <input type="checkbox" id="selectTwitter" name="selectTwitter" checked=@twitterSelected.ToString ()>I'm Twitterpated!</input><br /> <input type="checkbox" id="selectTwitter" name="selectTwitter" checked="@twitterSelected">I'm Twitterpated!</input><br /> 

UPDATE 4

I even tried this:

 <input type="checkbox" id="selectTwitter" name="selectTwitter" checked=@Convert.ToBoolean (twitterSelected)>I'm Twitterpated!</input><br /> 

... but it does not diff - the checkboxes simply will not respect vals cookies. Or rather, vals are not set: after unchecking all the cookies are "True", while one unverified must be false ...

Another attempt I made was because I saw that one of the cookies (secure_session) had the value β€œtrue” - adding this code when it was important:

 if (IsPost) { Response.Cookies["selectTwitter"].Value = twitterSelected.ToString().ToLower(); . . . 

... again, alas, in Mudville there is no joy: a powerful case, crossed out.

UPDATE 5

The problem is in my [il] logic: the IsPost / event / condition section does not set the cookie value of the current reality (checked state of the flag).

I tested by forcing val so in the if if (IsPost) {} "section:

 Response.Cookies["selectTwitter"].Value = "false"; 

... and it works great. But how to make it really work remains a mystery. Is there a jQuery event "httppost" where I can set all cookies based on the state of the flags? Or is there a checkboxChange or -Toggle event where I can set the cookie flag for the "this" flag?

UPDATE 6

I tried this too:

 if (IsPost) { Response.Cookies["selectTwitter"].Value = Request["selectTwitter"]; //"false"; . . . <form method="POST"> . . . <input type="checkbox" id="selectTwitter" name="selectTwitter" checked=@twitterSelected >I'm Twitterpated!</input><br /> 

... but still not; it’s not a problem that the name / identifier of the text control matches the name of the cookie value, is it?

+7
source share
2 answers
+2
source

As the error tries to tell you, selectTwitterCookie is an instance of an HttpCookie containing cookie information.

It makes no sense to convert this to a logical one.

You may need the Value property, which returns a string with the actual value of the cookie.

+1
source

All Articles