title: ASP.NET Send Cookies to client Browser
body: <p>I'm integrating a single sign on over 2 ASP.Net applications. For that matter i have a web service that is called by the main app. when a user logs in. this web service authenticates the user in my second application and brings back the authentication cookies i need to deliver to the client browser so he can navigate freely and logged in both applications.</p>
  
  <p>I was planning to use <strong><em>HttpContext.Current.Response.Cookies.Add(cookie)</em></strong> in order to deliver the new cookies but this seems not to work as no cookies are added what so ever...</p>
  
  <p>Any ideas on what might be going wrong?</p>
  
  <p>here is my code:</p>
  
  <pre><code>var service = new localhost.UserManagement();
  service.CookieContainer = new CookieContainer();
  if (service.AuthenticateUser("test@user.pt", "test"))
  { 
      var collection = service.CookieContainer.GetCookies(new Uri("http://localhost"));
      foreach (Cookie item in collection)
      {
          HttpContext.Current.Response.Cookies.Add(CookieConverter(item));
      }
      HttpContext.Current.Response.Flush();
      return true;
  }
  
  return false;
  </code></pre>
  
  <p>Note: <strong><em>CookieConverter(item)</em></strong> is used to convert Cookie object i receive to HttpCookie</p>
  
  <p>Thanks</p>
  
  <pre><code>
  private HttpCookie CookieConverter(Cookie cookie)
          {
              var result = new HttpCookie(cookie.Name);
  
              result.Value = cookie.Value;
              result.Domain = cookie.Domain;
              result.Expires = cookie.Expires;
              result.Path = cookie.Path;
              result.Secure = cookie.Secure;
              result.HttpOnly = cookie.HttpOnly;
  
              return result;
          }
  </code></pre>