2

I'm working in ASP.NET Core 6 and what I am trying to do is: set my list to session then get session info. But although there is no problem when setting it to session. When I try to get from session with GetString, it always returns null.

  1. ConfigureServices in Startup.cs:

enter image description here

  1. Configure in Startup.cs:

enter image description here

  1. UserRepository.cs:

enter image description here

  1. Code block in the UserService.cs that I set session and then try to get:

enter image description here

3
  • 2
    Please post your code as properly formatted text - do NOT post images of code .... Commented Apr 7, 2023 at 7:45
  • You can check the session id from when you are setting and getting and make sure it is same. if not same you might want to explain your flow for us to help. Commented Apr 7, 2023 at 7:55
  • I tested your code but it works as normal. You can try to see if the session id is the same. Commented Apr 7, 2023 at 13:18

1 Answer 1

0

I highly recommend you to not using Sessions in WebApi projects. because its not very common.

Read this to see how session works.

This why you always getting null, because you are not sending SessionId back to it, and he says Oh a new User 😉 lets create a new session for him...

In a brief description, When you are using Sessions, your client should keep an id (called SessionId) and send it back to your API using HttpHeaders. this is done automatically when you have a Web Application and web browsers are aware of it, they keep this logic encapsulated so you can easily use it.

If you are forced to use sessions in WebApi read this answer:

Note: good practice is to writing a middleware and using an AccessToken to work as a SessionId for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.