0

Is it possible to access a C# created cookie via Javascript and change the value? (I'm aware that it's a security measure - but whats the alternative)

C#

 HttpCookie myCookie = new HttpCookie("theName", "theValue");
 Response.Cookies.Add(myCookie);

Javascript

console.log(document.cookie) //doesn't include my cookie..
8
  • if its HTTP cookie then you cant access them in javascript. Yes you can access the cookie and change the value in javascript. Commented Mar 26, 2015 at 13:26
  • Is there an alternative way to create a cookie, using c#, without restricting javascript access? Commented Mar 26, 2015 at 13:27
  • Where and when are you adding the cooking in C#? Page_Load? Commented Mar 26, 2015 at 13:27
  • @jtimperley Global.asax Commented Mar 26, 2015 at 13:28
  • A cookie is a cookie, it is the same to create it from your C# code or from Javascript :) So of course you can access it wherever you want (C# or JS) Commented Mar 26, 2015 at 13:28

1 Answer 1

1

HttpCookie is by default protected - i.e. Javascript cannot read them. Setting the following bool property allows access.

myCookie.HttpOnly = false
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.