I am using ASP.Net Identity and wanted to add the ApplicationUserManager
service to all of my custom controllers by following this article: How to plug my Autofac container into ASP. NET Identity 2.1
This works perfectly in my controllers, but not when I try to create a token by calling localhost:xxxx/token on my API. Below is the method called, however the context.OwinContext.GetUserManager
returns null.
I have tried injecting the ApplicationUserManager
into the ApplicationOAuthProvider
, but was not able to successfully. Can you please point me in the right direction?
Edit: 10/15
Okay, so I have gotten a bit further, but I am still stuck.I was able to initialize the classes with the following:
var x = new DatabaseContext();
var store = new UserStore<ApplicationUser>(x);
var options = new IdentityFactoryOptions<ApplicationUserManager>()
{
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
};
builder.Register<DatabaseContext>(c => x);
builder.Register<UserStore<ApplicationUser>>(c => store).AsImplementedInterfaces();
builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => options);
builder.RegisterType<ApplicationUserManager>();
builder.Register<ApplicationOAuthProvider>(c => new ApplicationOAuthProvider("self", new ApplicationUserManager(store, options))).As<IOAuthAuthorizationServerProvider>();
This allowed me to pass the ApplicationUserManager
into my ApplicationOAuthProvider
's constructor. In the Startup.Auth
configuration, I initialize the Provider with the following:
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = (IOAuthAuthorizationServerProvider)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IOAuthAuthorizationServerProvider)),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
This gets me closer to a solution, but still has two problems.
The first is when I call /token on the API the userManager.FindAsync(context.UserName, context.Password)
returns a null value, but userManager.FindByEmailAsync(context.UserName)
returns the correct user. My initial thought is the password is wrong,but I made sure it was the same password I registered with.
The second issue, is if I call register on my AccountController, and then call /token, I get a Cannot access a disposed object.Object name: 'UserStore' error. So I assume this means I am not initializing the ApplicationOAuthProvider
correctly in my Bootstrapper file.
Any guidance would be greatly appreciated. Thanks!