Thursday, July 11, 2013

How to enable Web API Session feature in Windows authentication Web Application

Recently i use javascript to call Web API method to set and load the object store in the session in IIS server.

when i try to run the application, I never was able to retrieve the object from the session, when i debug the method in the web api controller, it indicated that the session was always null.


 since the Web API had disabled the session by default, we can use the following code to force the Web API to support the session.

 protected void Application_PostAuthorizeRequest()
 {
    if (IsWebApiRequest())
    {
       HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
    }
 } 
 
private static bool IsWebApiRequest()
{
   return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api");
 }

after i put the above code in the Global.asax file, the session objects are enable.

 Now i can save the object in the session and update or retrieve from the session.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  

No comments:

Post a Comment