I have a web-service, ‘AHandyWebService’ implemented within a .aspx (not within the .aspx.cs).
I’m trying to set a cookie on the client when the web-service is called.
I’m not sure whether this is just intrinsically impossible or whether it’s something to do with the way I’ve done it.
Because the method implementing the w-s is static I’ve had to implement a local instance of ‘page’ which I’m not sure is quite as it should be.
Method looks like this :
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public static SomeObj AHandyWebService()
{
SomeObj mySomeObj;
try
{
mySomeObj = getSomeObj();
System.Web.UI.Page p = new Page();
HttpCookie appCookie = new HttpCookie("FOOAPP");
appCookie.Value = String.Format("Written: {0:yyyy-MM-dd}", System.DateTime.Now.ToUniversalTime());
appCookie.Expires = System.DateTime.Now.ToUniversalTime().AddMinutes(1);
appCookie.Path = "/FOO";
p.Response.Cookies.Add(appCookie);
}
catch (Exception ex)
{
throw;
}
return mySomeObj;
}
By the way there are no cross-domain issues here – the w-s is provided from the same domain as the w-s consuming page was served from.
Would welcome suggestions/comments.
OK this is a bit embarrassing. After a lot of trying different things I discovered the method supporting the w-s was not in fact the method I thought it was ! … hence no cookie served !
Meanwhile in other news I came across this StackOverflow question which provided what seemed to me a more sane method of getting around accessing the Response object from a static method and so I have implemented that and it now works as it should.
In case it’s of some use here’s a copy of the now working method: