Asked by:
Callbacks in CreatePerOwinContext() never called - what's wrong?

Question
-
User2103511619 posted
In Startup.cs I am using Lambdas to create an instance of my <samp>UserManager</samp> class:
app.CreatePerOwinContext(() => new IdentityContext("AuthDatabase"));
app.CreatePerOwinContext((IdentityFactoryOptions<UserManager> options, IOwinContext owin) => new UserManager(new UserStore<User, Role, Guid, UserLogin, UserRole, UserClaim>(owin.Get<IdentityContext>()), options.DataProtectionProvider));In one of my controllers I'm using a Lambda to retrieve the <samp>UserManager</samp> instance:
private UserManager UserManager => _userMan ?? (_userMan = Request.GetOwinContext().GetUserManager<UserManager>());
But this line doesn't execute above Lambdas. Instead <samp>GetUserManager()</samp> always returns <samp>null</samp>.
What's wrong with above code? Why are the callbacks never called?
Tuesday, December 1, 2015 7:39 PM
All replies
-
User614698185 posted
Hi AxelD,
CreatePerOwinContext<T> method is invoked once per request and used to obtain instance object which is used during the lifetime of the request.
1.Instead of directly working with UserManager<T> class we can define a custom class, ApplicationUserManager that extends from UserManager<T>. In the project under the App_Start folder create a new file IdentityConfig.cs and add a new class ApplicationUserManager. The ApplicationUserManager should extend the UserManager class:
public class ApplicationUserManager : UserManager<ApplicationUser> { }
2. To create a static callback method that returns an instance of DbContext , in the ApplicationDbContext class create a method as defined below:
public static ApplicationDbContext Create() { return new ApplicationDbContext(); }
3. Similarly define a method in the ApplicationUserManager that returns an instance of the ApplicationUserManager.
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); return manager; }
4. Register these two callback methods in the ConfigureAuth method through the ‘CreatePerOwinContext’ method
public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext<ApplicationDbContext>(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); }
For more information, please see this blog:
Per request lifetime management for UserManager class in ASP.NET Identity
Best Regards,
Candice Zhou
Wednesday, December 2, 2015 3:18 AM -
User2103511619 posted
Thank you Candice,
I know. That's how the sample is implemented. Yet, I want to use lambdas. And I wonder why this doesn't work although it should. That's my concern here. Why doesn't this work?
PS: Hotmail's troubling with receiving notifications from www.asp.net: I either don't receive a notification or it's getting bumped into the Junk folder. But I never receive them to the Inbox. That's why I am replying so late.
Sunday, December 6, 2015 9:15 PM -
User614698185 posted
Hi AxelD,
And I wonder why this doesn't work although it should.You should make sure your Lambda expression is correct.
Hotmail's troubling with receiving notifications from www.asp.net: I either don't receive a notification or it's getting bumped into the Junk folder. But I never receive them to the Inbox.The latest version of the website doesn't have email notifications.
Best Regards,
Candice Zhou
Tuesday, December 8, 2015 9:38 AM -
User2103511619 posted
You should make sure your Lambda expression is correct.Would you mind elaborating on "is correct"?
I don't have compile errors. And no exception is thrown at run-time. I can't find an error here ... Do you?
Thursday, December 10, 2015 7:17 PM -
User614698185 posted
Hi AxelD,
You create the sample like in my original post document, if it works well, it proves your lambda expression has problem. If it still doesn't callback, there will other problem.
Best Regards,
Candice Zhou
Friday, December 11, 2015 7:31 AM