3

I'm fairly new to OWIN and I've implemented a solution that uses AspNet.Identity. In Startup Configuration there are several coded lines similar to the following...

        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

Now each object (ApplicationDbContext, ApplicationUserManager etc) has a Create method that takes an option parameter and the Owin context. What I'm not sure about is at what point the Create method is called, the life span and scope of the object. The following is what I believe to be the case but can anyone tell me if I have or have not understood it correctly...

  1. Startup.Configuration is called just once when the application is started.
  2. ApplicationUserManager will be created per request
  3. Doing HttpContext.GetOwinContext().Get() will get the same ApplicationUserManager object each time it's called, it won't create a new one each time.
  4. All objects that I Get in this way would be disposed of at the end of each request.
  5. Objects do not persist between requests.

Statement 5 is my real point of interest here. How would I go about having session specific objects say if I wanted to hold an ApplicationUser object whilst a user is logged in without having to get it from the database each time? Also can I store application specific objects say for caching purposes?

Here's a link to where I came up with this understanding but it doesn't talk about Session or Application scope...

http://blogs.msdn.com/b/webdev/archive/2014/02/12/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity.aspx

Since writting this I just realised that HttpContext has Session and Application properties which would be where to put those scoped items.

Hoots
  • 1,876
  • 14
  • 23
  • Use the `UserManager` class and let it worry about caching user objects. Any application objects, you will have to decide yourself how to cache them. – DavidG Aug 05 '14 at 16:03
  • 1
    UserManager does data access, it doesn't store the logged in user. It also gets disposed at the end of each request as far as I understand it so couldn't deal with session information. – Hoots Aug 05 '14 at 16:15
  • Regarding "HttpContext has Session and Application properties" that's not quite the answer you (or I) were looking for. Those properties are specific to the "traditional" ASP.NET architecture (prior to OWIN). As far as I can tell OWIN doesn't have support for object with Session or Application lifetime, just Request lifetime. – stefann Oct 23 '14 at 02:12

0 Answers0