0

In production mode I have the angular 5 client and the web api on the same site so I have no problem with the session but in development mode my angular 5 is hosted on localhost:4200 which reset the session on each call to a service.

I can host the angular 5 and the web api on the same host on development mode but it means I have to do ng build all the time(which will have a big overhead).

Any ideas? Should I use the session? or there is a better way?

update

First of all just like Andrei Dragotoniu said the use of session in the web api is not best practice and should be avoided.

But if the web api must use the session I have found the solution for the development mode - add a proxy to the angular 5 project: I had to proxy my requests to my web api server and than the requests to the web api and the web api are is the same domain.

how to add a proxy to the angular 5 project:

angular-cli server - how to proxy API requests to another server?

noah lubko
  • 99
  • 2
  • 10

1 Answers1

0

if you are talking about the dot net back-end session then no, you should not be trying to use that. The reason is simple, an api is stateless, each request is separate and needs to have all the data required to complete it.

On the client side however, you can use something that allows you to store stuff for later use, the local storage is a good candidate for that.

Also, in angular, you can share data using singleton services : https://angular.io/guide/singleton-services

These are services which are only create once, you can store data in them, inject them in whatever component needs that and they really become a way to share data. If you happen to hard reload your application, all that data will be lost so in such scenarios local storage can help.

I suspect you will end up with a combination of singleton services and local storage, but do stay away from trying to use session in a stateless api.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • by the way, my data is secure and I can't store it on the client side – noah lubko Jun 23 '18 at 08:34
  • @Andiei you are wrong about session. According to your concept there is nothing called session. Whether its mvc or api, every request is stateless, so server side Session concept is a hoax as per you. Further there is nothing called Cookie based authentication, I believe you know that authentication takes place at server side. – Sujoy Oct 01 '21 at 06:33