I'm developing an Asp.net (MVC but this doesn't really matter) application. I have a custom IHttpModule that's responsible for the PostAuthenticateRequest to change user principal & identity.
I'm storing UserID and UserName in authentication cookie when user logs-in. I have an IUser (implemented by DAO and Business Objects layer, each with their own additional members) that I need all over Business Service classes. When a user wants anything I have to provide IUser object instance (usually from Business Objects layer) so providing ID from the auth ticket isn't sufficient.
So I'm thinking of how and where would be best to persist logged in user's IUser data?
- I don't want to fetch it every time from the DB (based on authentication ticket's UserID data)
- I can't store it in Session since I have to work inside PostAuthenticateRequest, where Session isn't ready yet
- I want all the functionality to be encapsulated within my custom IHttpModule
Choices that I see:
- Cache
- Cookie
- (Session) - by moving from PostAuthenticateRequest to PostAcquireRequestState event and change principal/identity there, but I'd like to avoid this
Processes that seem to complicate things are:
- User logs-in, user data is fetched from the DB and persisted somehow for later requests
- User logs-out, user data has to be removed from persisted medium automagically
- User changes own profile, user data has to be discarded and reread on next request from the DB
I wan't all these to be handled automatically by HttpModule (if possible) to eliminate developer's errors of forgetting to reset these things.
What I also don't want is to write/read some hardcoded variables/keys and manipulate them in other parts of the application. This would only present technical debt.
Questions
- What would you suggest?
- How does SO persist user data between requests?