我正在尝试从我的 azure Webjob 为我的 mvc 应用程序运行清理作业。我可以毫无问题地进行标准数据库更新,但无法清理 aspnetuser 帐户,因为我无法获取 ApplicationUser 上下文,因为 Owin 没有启动类。
有人对如何做到这一点有任何想法或一些虚拟代码吗?到目前为止,我的谷歌搜索结果都是空白。
谢谢。
最佳答案
我正在使用 VS 2015 运行 ASP.net MVC 5
实际上,即使 ApplicationUserManager
需要 OwinContext
,您也可以摆脱 OwinContext
。
OwinContext
仅用于访问绑定(bind)到 OWinContext
的 ApplicationDbConbtext
。
因此,我重构了 ApplicationUserManager
的 Create
工厂方法,以便能够在启动时(或在 WebJob 中)管理用户,而无需任何 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
}
然后我使用 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;
}
}
希望对你有帮助
关于asp.net-mvc - 来自 azure Webjob 的 Identity 2.0/Owin/ApplicationUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36108105/