Write cookies from subdomain and read from another subdomain without changing web.config

I have a simple question. I have two different projects like

http://login.mydomain.com

and

http://test.mydomain.com

. As it’s name suggests, I login from login project and response redirect to test project.

I am able to create cookie for login.mydomain.com but I can not read it from test.mydoamin.com.
My question is can I create cookie from login.mydomain.com to www.mydomain.com and read it from test.mydomain.com as if I am reading it from www.mydomain.com.

This is how I create my cookies.

Response.Cookies["UserValidForMyDomain"].Value = myvalue;
Response.Cookies["UserValidForMyDomain"].Expires = dtExpireDate;

and how I read them.

string myValue = Request.Cookies["UserValidForMyDomain"].Value;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

No, but you can create a wildcard cookie for the domain of .mydomain.com which will allow any subdomain to read/write it.

Method 2

TO WRITE

HttpCookie hc = new HttpCookie("key", "value");
hc.Domain = ".mydomain.com";
hc.Expires = DateTime.Now.AddMonths(3);
HttpContext.Current.Response.Cookies.Add(hc);

TO READ

HttpContext.Current.Request.Cookies["key"].Value

Method 3

In php this will do session_set_cookie_params

In asp and equivalent, these stackoverflow thread might be usefull

How to share session among Multiple Domains on single asp.net website?

How can you keep a session across multiple subdomains in c# mvc?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x