Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am try to open URL on IE11 using C# console application. It is OK but I also want to set the cookie before open that URL on IE11. I saw this question and do exactly as given in answer

here my code in inside main()
C#
if (InternetSetCookie(webUrl, "mycookie1", "myCookieValue"))
{
    Console.WriteLine("1st Cookie is sucessfully created.");
}

if(IESetProtectedModeCookie(webUrl, "mycookie2", "data=mycookiedata; expires = " + DateTime.UtcNow.AddDays(1).ToString() + "; path=/", 0x10))
{
    Console.WriteLine("2nd Cookie is sucessfully created.");
}

Process.Start("IExplore.exe", webUrl);


both methods are return true and display the messages. But when I try to see the cookie on IE11 using developer tool console by typing document.cookie then undefined message occur. Please help me.
Posted
Comments
F-ES Sitecore 25-Jun-15 8:57am    
Use the network tab in the browser tools to see what cookie data, if any is being passed. One thing it might be is the expiry value; your value is not in the required format. You have to convert to UTC format after you've added the day. You are simply doing "ToString" which is converting it to a format specified in your Windows globalisation settings, and almost certainly won't be UTC.

1 solution

Cookies are a PITA - IE cookies aren't ready by Firefox, or Chrome; Chrome ones aren't read by IE or Firefox; and Firefox cookies aren't read by IE or Chrome.

But that is as nothing to what you are trying to do.
If you look at the documentation for InternetSetCookie[^] it says very clearly:

"Cookies created by InternetSetCookie without an expiration date are stored in memory and are available only in the same process that created them. Cookies that include an expiration date are stored in the windows\cookies directory."

And IESetProtectedModeCookie[^] if very sepecific about the date time format it requires:

"To create a persistent cookie, add an expires name/value pair to the cookie data. The format of the value must be DAY, DD-MMM-YYYY HH:MM:SS GMT. DAY is a three-letter abbreviation for the day of the week, DD is the day of the month, MMM is a three-letter abbreviation for the month, YYYY is the year, and HH:MM:SS is the time of day in 24-hour format."

So your cookies need an expiry date in order to go into the IE cookie cache, since IE will run in a different process. And the persistent ones need a very specific date format!

But...if your user doesn't have IE installed (and that's pretty likely these days) you can't do anything that will work!
 
Share this answer
 
Comments
Rahul Kumar 26-Jun-15 3:00am    
thanks for reply. I try the things that you mentioned but still not able to set the cookie and yes I just want to set for IE11.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900