0

I am trying to write a cookie from .exe app and then read it from JavaScript. is it possible?

exe coode:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetCookie(string url, string name, string data);
static void Main(string[] args)
{
    InternetSetCookie("http://www.example.com", "CookieName", "data=cookieData; expires = Sat,01-Jan-2012 00:00:00 GMT; path=/");
}

and JavaScript code:

alert(getCookie("CookieName"));

this prints:

undefined

where get cookie function is:

function getCookie(Name) {
    var search = Name + "=";
    if (document.cookie.length > 0) { // if there are any cookies 
        var offset = document.cookie.indexOf(search);
        if (offset != -1) { // if cookie exists 
            offset += search.length;
            // set index of beginning of value 
            var end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value 
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(offset, end));
        }
    }
}
spez
  • 409
  • 8
  • 21
  • I dont know about that C# part, but cookies are set in the browser. Does the C# part specify the browser anywhere? – MauriceNino Jul 28 '20 at 10:41
  • Also here is a shorter function for reading a cookie in JS: https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript – MauriceNino Jul 28 '20 at 10:42

0 Answers0