I've tried the instruction from this answer to enable sessions on my API calls with a little change since I do not use the MVC WebApi but use an area instead. Here is my Global.asax.cs
...
protected void Application_PostAuthorizeRequest()
{
if (IsWebApiRequest())
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
}
private bool IsWebApiRequest()
{
return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("/ws/");
}
And here is the code I use to call the web service from a different web application.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
return Newtonsoft.Json.JsonConvert.DeserializeObject<SessionReturn>(reader.ReadToEnd());
But whenever I make a call, the session always resets. I even try to remove the checking if it is a WebApiRequest()
and even add a [SessionState(SessionStateBehavior.Required)]
attribute to my api controller but to no avail. Is there anything I'm doing wrong?