1

My requirements - I have a web api whch gives me all data from db. I have a .net website which consumes this api to get all data. Now what I want is when I'm login my website I want to manage session in "API".

I know session in web api is not a good approach but still I need to do this.

I have already implemented session management in web api(taken reference from here) and its working fine if I'm sending all my request from postman(i.e. I'm setting variables in session by calling 1 method and retrieving that session variable by calling 2nd method). But when I'm doing the same from asp.net website with jQuery then I'm not getting stored session variable(what I noticed is I'm getting session id different every time-for each request).

code of saving variable in session

    $.ajax({
                url: 'http://localhost:63726/api/Login/Login',
                type: "GET",
                dataType: "JSON",
                success: function (data) {
                    alert("success");
                },
                error: function (data) {
                    alert("error");
                }
            });

code of retrieving variable stored in session

    $.ajax({
                url: 'http://localhost:63726/api/SessionCheck/LoginName',
                type: "GET",
                dataType: "JSON",
                success: function (data) {
                    alert("success");
                },
                error: function (data) {
                    alert("error");
                }
            });

What I need to do to achieve my goal..Your opinion will save my days...

B R
  • 51
  • 1
  • 10

2 Answers2

1

I found my answer Session management in web api.

B R
  • 51
  • 1
  • 10
0

Please, read this Web Api Session storage before.

To retreive data from session use javascript sessionStorage (instead ajax).

Manfice
  • 149
  • 7
  • I've already read that. And issue is not with jquery code.Issue is that when I'm calling web api in web api I found different session id. If I'm not getting you then please elaborate.. – B R Sep 11 '17 at 07:47
  • At first, Web Api is stateless by default. If you wish (or need) store data in browser session storage you must resolve it on client side via js. If you with resolve it on server side read this [WebApi session](https://www.strathweb.com/2012/11/adding-session-support-to-asp-net-web-api/). – Manfice Sep 11 '17 at 12:11
  • I've already read that article too and that's working good. My issue is to maintain same session id while calling api...which is actually running successfully with postman and direct request from browser. – B R Sep 11 '17 at 12:32