-1

Normally Cookies are stored in the following location:

C:\Users\SomeUser\AppData\Roaming\Microsoft\Windows\Cookies

What i want to do is write cookie in a text file at this location using c# which is quite simple no problem at all, this is ok.

But i am not able to find a way to read cookie from that file using java-script in my web application.

Is it even possible?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Is the cookie you are writing from the same web application that will be reading it via JavaScript? If so, there shouldn't be any issues that the 2 answers below point out. you really should try google first: http://stackoverflow.com/a/2905162/3036342 – user3036342 Mar 16 '15 at 07:32
  • @user3036342 i want to write cookie in text file in the specified location from a windows form application and on the same machine when user open a web application i want to read that file using javascript on client machine – Ehsan Sajjad Mar 16 '15 at 07:34
  • Why would you want to do that? If you tell me the why we can possible steer you in a direction where it will be possible. Unfortunately what you want to do won't work. If it did, spammers/advertisers would have a field day. – user3036342 Mar 16 '15 at 07:37
  • i want the user to be automatically logged in if user is already logged in my desktop application, using cookies i want to pass some information of logged in user to the browser – Ehsan Sajjad Mar 16 '15 at 07:57
  • Look at Anant Dabhi's answer. This is what you would need and I have done in the past before – user3036342 Mar 16 '15 at 10:47

4 Answers4

1

JavaScript has no privileges reading files from the disc. You can't make a cross-domain request from a local file because it's not on a domain. You need to host C:\index.html on a local webserver instance in order for it to work. Or start Chrome with the following parameters.

--disable-web-security -–allow-file-access-from-files

You should also add the following to your service response:

response.setHeader("Access-Control-Allow-Origin", "*");
Bruniasty
  • 418
  • 5
  • 18
0

I believe it is possible, but due to the security issues it's won't be straight forward.

Check this jQuery lib: https://github.com/carhartl/jquery-cookie


Note: Some browsers for eg. Chrome, restrict from using coockies from file : Why does Chrome ignore local jQuery cookies?

Community
  • 1
  • 1
Piotr Dajlido
  • 1,982
  • 15
  • 28
0

I think u looking for this answer that u mention on comment..

i want the user to be automatically logged in if user is already logged in my desktop application, using cookies i want to pass some information of logged in user to the browser

. For that you need to generate a token that can be used for a one-time login that is stored in the database and Make sure tokens expire if they haven't been used in e.g. 8 hours. When you start the website, pass the token on the query string to the website. Then the website uses the token to connect the user's session to their account, and the token gets deleted.

Also you can use Single sign on WIKI

Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
0

After struggling alot and searching, i found a way to do it. We can use InternetSetCookie method which is in wininet.dll to set cookie for a specific url or web application.

Add this code in your class:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetCookie(string url, string name, string data);



public static bool SetWinINETCookieString(string url, string name, string data)
{
   return Form1.InternetSetCookie(url, name, data);
}

Usage:

var cookieOutput = SetWinINETCookieString("http://localhost:49549/Home/Index", "dataToTest", "thisIsTheData;Expires = " + DateTime.Now.AddDays(10).ToString("R"));

For testing purpose i hardcoded my application specific url.

and then in web application i wrote this javascript to get the cookie value:

$(document).ready(function () {

            alert(getCookie("dataToTest"));

});

and here is getCookie function:

function getCookie(name) {
            var value = "; " + document.cookie;
            var parts = value.split("; " + name + "=");
            if (parts.length == 2) return parts.pop().split(";").shift();
        }

This SO post really helped me out.

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160