Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

HttpCookie vs Cookie, and multi-value HttpCookie

$
0
0
I don't get it here. I've always used HttpCookies to store multiple values in them
But using Cookie, it seems you can only store 1 value.

What I'm trying to do:
I have a mvc view called Bug Report. You click the button and it runs the Diag Controller with an action that uses HttpWebRequest and prior was WebClient. I changed it to transmit a cookie in the header, because WebClient was too basic.

On the page that is getting read, I have an home made security attrubute that uses OnActionExecuting and the ActionExecutingContext. If the request doesn't contain the session variable, it gets kicked out to Logout.

So to the best of my research, you can make a cookie container with multiple cookies.
This works fine, but I wanted to refine it more...
With HttpCookies, I can send 1 cookie with the 4 values, but with cookie, I have to send each cookie separate. I don't understand this part.
CookieContainer container = new CookieContainer();<br />
container.Add(new Cookie("a", httpCookie["a"]) { Domain = target.Host });
container.Add(new Cookie("b", httpCookie["b"]) { Domain = target.Host });
container.Add(new Cookie("c", httpCookie["c"]) { Domain = target.Host });
container.Add(new Cookie("d", "true") { Domain = target.Host });var webRequest = (HttpWebRequest)WebRequest.Create(rUrl);
webRequest.CookieContainer = container;using (var response = (HttpWebResponse)webRequest.GetResponse())
{
    model.Page_HTML = response.ToString();
}

So I wrote this, the main cookie, but I can't figure out how to store 4 records in it.
Cookie cookie = new Cookie() {
   Name = "Main",
   Expires = DateTime.Now.AddMinutes(1),
   Domain = target.Host,
   HttpOnly = true,
   Secure = true
}; 

On the OnActionExecuting side, I figured how to read the cookies. But it seems sort or silly to read 4 cookies. It looks like on this side I can just read a HttpCookie of any format.
HttpCookie cookie_A = filterContext.RequestContext.HttpContext.Request.Cookies.Get("a");

So my question is:
Am I beating a dead horse with a stick here and I should just be happy that the 4 cookies work?
If it ain't broke don't fix it

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>