I have a asp mvc application consuming wcf rest services (all on the same box). For authentication calls, I am trying to set cookies inside a wcf rest service.
Code on the client side -
HttpResponseMessage resp;
HttpClient client = new HttpClient("http://localhost/auth/login/");
resp = client.Get();
In the webservice I just use FormsAuthentication to set an authcookie.
HttpCookie authCookie = FormsAuthentication.GetAuthCookie("foo", false);
HttpContext.Current.Response.Cookies.Add(authCookie);
Assuming the credentials are hardcoded in the code - If I physically navigate to the browserpage
http://localhost/auth/login
(hard code credentials in the code) I can see that the authentication cookie is being set. However, if I just invoke it through code (as shown above) the authentication cookie is not being set.
Is there something obvious that I am overlooking here?
When you call the WCF service programatically the cookie it sets is stored in the HttpClient instance. The client browser never sees it so it is never set inside it. So you will need to set it manually: