Ich bin mit ASP.net MVC 5 mit VS 2015
Eigentlich, auch wenn die ApplicationUserManager
eine OwinContext
erfordert, können Sie loswerden der OwinContext
bekommen. Die OwinContext
ist nur nützlich für den Zugriff auf die ApplicationDbConbtext
, die an die OWinContext
gebunden ist.
So Refactoring ich die Create
Fabrikmethoden ApplicationUserManager
, um den Anwender bei der Inbetriebnahme verwalten zu können (oder in einem WebJob), ohne OwinContext
:
public static ApplicationUserManager Create(
IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
ApplicationDbContext dbContext = context.Get<ApplicationDbContext>();
return Create(options, dbContext);
}
internal static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, Applicatio
{
// Create ApplicationUserManager myself instead of finding it on OwinContext
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
// old code already generated by VS wizard
}
Dann benutze ich die ApplicationUserManager
:
using (ApplicationDbContext applicationDbContext = new ApplicationDbContext())
{
var options = new IdentityFactoryOptions<ApplicationUserManager>
{
//DataProtectionProvider = app.GetDataProtectionProvider(),
};
ApplicationUserManager userManager = ApplicationUserManager.Create(options, applicationDbContext);
// ApplicationRoleManager has been changed the same way to be usable without OwinContext
ApplicationRoleManager appRoleManager = ApplicationRoleManager.Create(applicationDbContext);
// ... Code using ApplicationUserManager and ApplicationRoleManager
ApplicationUser appUser = userManager.FindByEmailAsync(email).Result;
// ...
IdentityResult result = userManager.CreateAsync(appUser, password).Result;
}
}
Ich hoffe, es hilft