1

Trying to set a domain in the cookie in asp.net and I am very novice in asp.net so wanted to know how this can be done.

here is my code

// Create cookie
var cookieData = new NameValueCollection();
cookieData["first_name"] = first_name;
cookieData["last_name"] = last_name;

var cookie = new CookieHeaderValue("UserInfo", cookieData);
cookie.Expires = DateTimeOffset.Now.AddDays(1);
//cookie.Domain = Request.RequestUri.Host;
cookie.Domain = "example.com";
cookie.Path = "/";

Need to set the cookie to work for srv1.example.com & srv2.example.com

currently running through Visual Studio and using Request.RequestURI.Host gives me the domain as localhost.

7
  • c# it is why what's wrong Commented Mar 1, 2013 at 21:44
  • That's not how you get a cookie's domain. Google it. Commented Mar 1, 2013 at 21:46
  • I used google before coming here because i did not find definitive answers on google Commented Mar 1, 2013 at 21:48
  • @user1833222 Just unclear when you say "currently running through Visual Basic" Commented Mar 1, 2013 at 21:49
  • 1
    Updated OP's question to clarify. Commented May 14, 2014 at 16:52

1 Answer 1

1

If the ASP.NET page that is setting this cookie was requested from http://localhost meaning that you are running your application either in Visual Studio or the local IIS, you cannot set the domain property to something different than that.

But if you deploy your application on a different server and access it from example.com then the domain property of the cookie will be correctly set.

Sign up to request clarification or add additional context in comments.

6 Comments

right but then why is it showing me localhost when i check the cookies in firefox
you are right I am running the code through Visual Basic running IIS locallly, so you are saying with the current code I have and setting the domain to example.com on production it should take the appropriate domain and sent the cookie correctly with the appropriate domain
I have updated my answer. Actually you cannot set the cookie domain to be different than the currently requested domain.
so what are you saying Darin
If you deploy your application to production and request this page from http://example.com or http://subdomain.example.com then the domain property of the cookie will be correctly set to example.com. So what I am saying is that your code is fine.
|

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.