entity-framework – 为什么ASP.NET SPA模板会为所有请求实例化UserManager一次?
|
我使用VS2013 ASP.NET SPA模板作为我的Web应用程序的起点,它使用新的ASP.NET身份框架.这来自模板: public partial class Startup
{
static Startup()
{
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
....
}
因此,由于没有DbContext被传递到上面的UserStore构造函数中,这向我指示正在创建新的DbContext.由于我还想利用数据上下文(对于请求期间的其他数据操作),我稍微更改了模板代码: public partial class Startup
{
public static DerivedDbContext=null;
static Startup()
{
context = new DerivedDbContext();
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(context));
...
}
现在,我可以通过以下方式从我的任何控制器使用相同的DbContext: Startup.context 但是,当多个请求同时进入时我遇到了麻烦,因为无关的操作发生在同一个DbContext中.从这里开始,我向我指出,我不应该在应用程序的整个生命周期中实例化单个DbContext,而只是为了特定请求的生命周期,所以我将实例化移动到控制器的构造函数,但是现在,我在控制器中有自己的DbContext,而UserManager仍然有自己的(并且在Startup类中创建). 为什么模板为所有用户(在Startup类中)实例化了一次UserManager?值得关注的是,通过在控制器构造函数中创建自己的DbContext,有两个DbContexts(我在控制器的构造函数中创建的那个,以及在UserManager中创建的那个,在Startup类中创建的)同时播放?在所有请求中共享一个UserManager是否可以接受,但是在一般情况下在所有请求中共享一个DbContext是不可接受的? 有两个单独的上下文似乎是愚蠢的,我注意到我有时会得到一个不同步的数据视图.我想知道是否有其他人在使用此模板时遇到此问题. **编辑:我理解像nInject这样的IOC框架可能有助于在这种情况下管理对象生命周期,但我想首先了解如何在没有这种框架的帮助下实现这一目标. 谢谢… 解决方法因此模式应该是每个请求一个UserManager和一个DbContext.在2.0-alpha1版本中,我们尝试通过添加一些新的身份中间件来解决这两个问题:我们正在开发一个更新的样本包来演示这些,但与此同时 您可以使用新软件包添加以下IdentityModels.cs / Startup.Auth.cs: public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create() {
return new ApplicationDbContext();
}
}
public class ApplicationUserManager : UserManager<ApplicationUser> {
// Configure the application user manager
public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store) {
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options) {
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(options.Context.GetDbContext()));
//var manager = new ApplicationUserManager(new AzureStore<ApplicationUser>());
manager.UserValidator = new UserValidator<ApplicationUser>(manager) {
AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true
};
manager.PasswordValidator = new MinimumLengthValidator(6);
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null) {
manager.PasswordResetTokens = new DataProtectorTokenProvider(dataProtectionProvider.Create("PasswordReset"));
manager.UserConfirmationTokens = new DataProtectorTokenProvider(dataProtectionProvider.Create("ConfirmUser"));
}
return manager;
}
}
public static class OwinExtensions {
public static IAppBuilder UseDbContextFactory(this IAppBuilder app,Func<DbContext> createCallback) {
if (app == null) {
throw new ArgumentNullException("app");
}
if (createCallback == null) {
throw new ArgumentNullException("createCallback");
}
app.Use(typeof(IdentityFactoryMiddleware<DbContext,IdentityFactoryOptions<DbContext>>),new IdentityFactoryOptions<DbContext>() {
Provider = new IdentityFactoryProvider<DbContext>() {
OnCreate = (options) => createCallback()
}
});
return app;
}
public static DbContext GetDbContext(this IOwinContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
return context.Get<DbContext>();
}
}
以下内容进入Startup.Auth.cs: public void ConfigureAuth(IAppBuilder app) {
// Configure the db context and user manager to use per request
app.UseDbContextFactory(ApplicationDbContext.Create);
app.UseUserManagerFactory(new IdentityFactoryOptions<ApplicationUserManager>() {
DataProtectionProvider = app.GetDataProtectionProvider(),Provider = new IdentityFactoryProvider<ApplicationUserManager>() {
OnCreate = ApplicationUserManager.Create
}
});
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider {
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,ApplicationUser>(
validateInterval: TimeSpan.FromSeconds(5),regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 在Response.Redirect()之后调用Response.End()
- asp.net – 将初始空行插入到选择列表MVC LINQ SQL中
- asp.net – ASP.MVC CORE网站的最大上传大小
- asp.net – Web部署项目的更好替代品
- asp.net – Html.BeginForm()类型的扩展名
- asp.net-mvc-4 – 使用WebApi和外部登录的ASP.NET身份
- 使用Gzip在ASP.NET / IIS7中输出乱码错误页面
- 在ASP.Net日历控件中默认选择当前日期
- iis-7 – 经典ASP站点请求在IIS7中随机挂起
- 连字符前分割字符串 – asp.net c#
- asp.net-mvc – 使用Entity Framework在控制器中
- [asp.net mvc 奇淫巧技] 02 - 巧用Razor引擎在Ac
- asp.net-core – ASP .NET Core 1.0 RTM本地化无
- iis – 如何从服务器上的经典asp页面执行bat文件
- ASP.NET MVC默认项目结构和脚本文件夹
- asp.net – 将提交请求提交到aspx页面
- asp.net – AspNetSynchronizationContext
- asp.net – 如何冻结GridView标题?
- IIS 7.5中未显示任何ASP.NET功能
- asp.net-mvc – 使ASP.NET MVC应用程序Web Farm准
