How to Access session from Web API

SOS Childrensvillages asked on May 22, 2018 10:06

HttpContext.Current.Session returns null when trying to access it from Web API. Is there a method I need to call in my OnInit method when registering my custom module so that I can access the session?

I'm using Kentico 11 and set up my Web API as per the instructions in the documentation

Correct Answer

Michal Samuhel answered on May 22, 2018 10:40

WebAPI on its own is RESTful. REST by design is stateless. By adding session (or anything else of that kind) you are making it stateful and defeating any purpose of having a RESTful API.

"The whole idea of RESTful service is that every resource is uniquely addressable using a universal syntax for use in hypermedia links and each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP". With that said,you can hack around with one of the following ways:

https://forums.asp.net/t/1780385.aspx

And better discussed and described:

https://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api

2 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.