0

I have a situation where I want to use a cookie with these 2 subdomains app.example.com (ASP.NET) and appapi.example.com (Web API). I'm able to set the cookie successfully using the API like this:

public HttpResponseMessage Get()
{
            ...
            var result = jsonHelper.setHttpResponseMessage(obj);

            List<CookieHeaderValue> cookies = new List<CookieHeaderValue>();
            NameValueCollection values = new NameValueCollection();

            values["Value1"] = value1;
            values["Value2"] = value2;
            values["Value3"] = value3;
            CookieHeaderValue cookie = new CookieHeaderValue("MyCookie", values);
/*#if DEBUG
                cookie.Domain = Request.RequestUri.Host;
#else*/
                cookie.Domain = ".example.com";  //I made this based on the answers to the questions posted below
//#endif
                cookie.Path = "/";
                cookie.Expires = DateTime.Now.AddHours(1);
                cookie.HttpOnly = true;
                cookies.Add(cookie);
                result.Headers.AddCookies(new CookieHeaderValue[] { cookie });

                return result;
}

I'm using Postman and I get a response that looks like this under Cookie tab:

Name         Value
MyCookie     Value1=2&Value2=test&Value3=val

Then I'm sending the cookie to app.example.com/Page.aspx using Postman. The code at Page.aspx Page_Load looks like this:

if (!IsPostBack)
{
    if (Request.Cookies["MyCookie"] != null)
    {
        var myCookie = Request.Cookies["MyCookie"].Values;
        var value1 = myCookie["Value1"];
        var value2 = myCookie["Value2"];
    }
...

Here I'm not getting the values that I want which means that Request.Cookies["MyCookie"] is null

I'm aware of this question and this one which I have implemented above, but still it didn't fix my issue.

Based on this, I believe that it's possible to use a cookie with 2 or more subdomains, so I need to know how to implement it properly.

Ahmed
  • 511
  • 3
  • 6
  • 23
  • 1
    i don’t think you did anything wrong, it was probably you did not attach the cookie to your postman request correctly. actually you even don’t need to set the cookie domain if you are using postman to test, of course for real deploy you must have it, what i am trying to explain is your problem is not related to your code but you test way, because the cookie domain is used by browsers to determine if they need to attached the cookie to the request, since you can manually set your cookie in postman, it doesn’t matter at all – Ray H Oct 02 '17 at 02:44
  • @RayH Can you please point out how a cookie should be sent using Postman? I'm adding it under **Headers** tab, **Key** = Cookie, **Value** = MyCookie – Ahmed Oct 02 '17 at 08:03

1 Answers1

1

https://www.getpostman.com/docs/postman/sending_api_requests/cookies

above is the official document of how to add cookie in postman

Ray H
  • 491
  • 2
  • 7
  • It was as you said in your comment, the issue was due to not sending the cookie correctly. That was because of not using Postman as a _native_ app and so, I only had **Code** button under the **Send** button. I have seen that docs links before but I assumed it was outdated documentation because I don't have the _Cookies_ button. It seems that we have **2 versions of Postman** (Chrome extension & Postman Native App). After downloading the native app, I could use **Cookies** button and the issue is resolved. – Ahmed Oct 02 '17 at 09:37
  • Kindly quote the steps in the link that you have shared so that I can accept your answer because external links _may_ get changed in the future. – Ahmed Oct 02 '17 at 09:37
  • @Ahmed never mind, the link is fine, i don’t think they will change the documents and it’s also easy to search. ;) – Ray H Oct 02 '17 at 13:49