Blazor generating single session id for user session #51704
Unanswered
SethSikoia
asked this question in
General
Replies: 1 comment
-
I think the answer you want is to register the class in the Di as Scoped, not as a singleton. Scoped instances are directly tied to the session so only live as long as the user's session. Just be aware that doing things like a page refresh will normally create a new session, so the same user would end up with a new Id in this scenario. There is a full example of what you are trying to do here: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a blazor server side application and I want to implement some logging logic to track user activity. I am using structured logging and I want to add a property that I can use to filter all logs related to a given user session.
The word session can have a different meaning given the context, what I mean in this case by session is one visit of a user to the application, so given then user opens a new tab that will start a new session.
We have created a service that will handle generating the session id and adding it as a property in the logger using Logger.BeginScope, based on our tests, none of the available service registration options seems to be fit for purpose. Singleton service seems to be global for the whole application so all user share the same session id, scoped service seemed to be the one that would work as supposedly there is one for each blazor circuit. However the behaviour exhibited in practice is that the scoped application service is instantiated more than once in the same user session, meaning that multiple session id's are produced.
What is the best way to guarantee the session Id will be consistent for the lifetime of the users session?
Beta Was this translation helpful? Give feedback.
All reactions