I just started with Autofac and trying to wire up Asp.Net Identity and I am failing. I thought I had everything working but I got stuck and need a hand.
The UserManager is injected via the constructor.
private readonly ApplicationUserManager UserManager;
private readonly ApplicationSignInManager SignInManager;
public UserController(IDocumentSession documentSession, ApplicationUserManager userManager, ApplicationSignInManager signInManager) : base(documentSession) {
UserManager = userManager;
SignInManager = signInManager;
}
My UserController.cs is having issues the moment I reach this async call.
var user = await UserManager.FindAsync(model.Email, model.Password);
This resolve operation has already ended. When registering components using lambdas, the IComponentContext 'c' parameter to the lambda cannot be stored. Instead, either resolve IComponentContext again from 'c', or resolve a Func<> based factory to create subsequent components from.
The issue is that I get a pretty clear message but I don't know how to continue with it; should I register my ApplicationUserManager in a different way?
I have setup my container like this. I hope someone can take a look.
public static void RegisterContainer(IAppBuilder app)
{
// Autofac container ..
var builder = new ContainerBuilder();
// Register all MVC Controllers
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register all services that implement the Module interface
builder.RegisterAssemblyModules(BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray());
// Register the DocumentStore instance
builder.Register(context => new RavenDocumentStoreFactory()
.FindOrCreate())
.As<IDocumentStore>()
.SingleInstance();
// Multi tenancy part
var tenantIdStrategy = new TenantIdStrategy();
var multitenantContainer = new MultitenantContainer(tenantIdStrategy, builder.Build());
var tenantIds = new[]
{
"subdomain1.tickets",
"localhost"
};
foreach (var tenantId in tenantIds)
{
var databaseName = $"ticketTenant-{tenantId.Replace('.', '_')}";
multitenantContainer.ConfigureTenant(tenantId, b =>
{
// Init RavenDB
b.Register(context => new RavenDocumentSessionFactory(databaseName))
.InstancePerTenant()
.AsSelf();
// Session per request
b.Register(context => context.Resolve<RavenDocumentSessionFactory>()
.FindOrCreate(context.Resolve<IDocumentStore>()))
.As<IDocumentSession>()
.InstancePerRequest()
.OnRelease(x =>
{
x.SaveChanges();
x.Dispose();
});
// ASP.Net Identity Registrations
b.Register(context => new UserStore<User>(context.Resolve<IDocumentSession>))
.AsImplementedInterfaces()
.InstancePerRequest()
.OnRelease(x =>
{
x.Dispose();
});
b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c =>
new IdentityFactoryOptions<ApplicationUserManager>() {
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
b.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
b.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();
});
}
// Register in Owin
app.UseAutofacMiddleware(multitenantContainer);
app.UseAutofacMvc();
// Dependency Resolver to Autofac
DependencyResolver.SetResolver(new AutofacDependencyResolver(multitenantContainer));
}
}
I solved my own issue, but honestly ... I haven't got a clue how I fixed this. It was just trial and error. This is the new configuration; the old part is commented.
It doesnt even need the last Register in Owin part... even commented it appear to work just fine now.
// ASP.Net Identity Registrations
/*
b.Register(context => new UserStore<User>(context.Resolve<IDocumentSession>))
.AsImplementedInterfaces()
.InstancePerRequest()
.OnRelease(x =>
{
x.Dispose();
});
b.Register<IdentityFactoryOptions<ApplicationUserManager>>(c =>
new IdentityFactoryOptions<ApplicationUserManager>() {
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
b.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
b.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();
*/
b.Register(c => new UserStore<User>(c.Resolve<IDocumentSession>())).As<IUserStore<User>>().InstancePerRequest()
.OnRelease(x =>
{
x.Dispose();
});
b.RegisterType<ApplicationUserManager>().InstancePerRequest();
b.RegisterType<ApplicationSignInManager>().InstancePerRequest();
b.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
b.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.