Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My application structure:
UI (HTML & angular JS) - Separate Host project. Lets consider the hosted URL is (http://localhost:1437/UI)
API (WEB API & C#) - Separate Host project. Lets consider the hosted URL is (http://localhost:1450/API)
Note: NO MVC

My all API calls from UI like normal http angular call with the reference of API url.

Now, I wanted to hold some session data(not large values, literally one integer Id) as per requirement. I can hold the session data only on API project, but not on UI project since no server side code on here.


I am able to achieve the session management in WEB API. but each and every time API call, new session creating when I call API from UI project. but calling API directly from browser URL, session data holds good.

I could understand, the problem because of the both applications are independent.

So, anyone can suggest how can I achieve the above problem.


thanks in advance! :-)

What I have tried:

I am able to achieve if both application hosted as it one!
But my application architecture is it must be 2 different hosts
Posted
Updated 1-Feb-19 0:16am
Comments
F-ES Sitecore 1-Feb-19 5:48am    
Have you tried google for "asp.net sharing session"? There isn't any out of the box solutions, you'll probably need to implement some kind of shared session provider yourself.
Prakash Thirumoorthy 1-Feb-19 6:03am    
but sharing session is depends on SQL. My application works both MySQL and SQL. So cannot depend on it. :-(
F-ES Sitecore 1-Feb-19 6:10am    
You can use in-memory session management too.
Prakash Thirumoorthy 1-Feb-19 8:26am    
I will try this and let yu know
MadMyche 1-Feb-19 5:54am    
Does your UI project accept Cookies from the API?

1 solution

Looks like the problem is that the UI portion of this is not accepting the Session Cookie from the WebAPI.
When the API does not receive the Session Cookie from a client, the API will start a new session when it receives a request.

What you will need to do then is to enable cookies in the UI portion of your project.
MS Docs does have guidance on this: HTTP Cookies in ASP.NET Web API | Microsoft Docs[^]
There are also some third party GIT packages such as CookieAwareWebClient that may make this easier

Addendum 1
Cookieless sessions were once the answer but are no longer supported. They also made really ugly URLs as they tacked on a query string to ID the session
ASP.NET MVC and using cookieless sessions - Stack Overflow[^]

Addendum 2
Upon re-reading your original post, I see you only are going to be dealing with "literally one integer ID", what you could do is send that data out as a response header (encrypted if needed) and have the calling UI receive it and send it back in the request headers.
This is how some APIs work keeping track of tokens for identification and authorization types of things
 
Share this answer
 
v2
Comments
Prakash Thirumoorthy 1-Feb-19 8:27am    
thanks for posting your answer.. but my application should not depend on cookie
MadMyche 1-Feb-19 9:57am    
HTTP is stateless, and how Session works is it sends out a cookie to identify the session on subsequent requests.

I am updating my answer with some more information.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900