代码优先迁移期间的UserTokenProvider / DataProtectorTokenProvider NullReferenceException种子方法

时间:2015-10-13 09:35:37

标签: c# asp.net-mvc entity-framework asp.net-identity owin

架构更新正常,但由于NullReferenceException,种子方法无法运行。其他一切似乎都很好,包括能够在网站运行时发出令牌以进行密码重置等。它只是在Seed方法运行时执行此操作。我想我在添加DataProtectorTokenProvider之后才开始这样做。有什么想法吗?

编辑:当然这个NullReferenceException根本就不会发生,所以What is a NullReferenceException, and how do I fix it?在这里无关紧要。

编辑:或者它可能不是。我错过了一些非常基本的东西吗?这行代码:

UserTokenProvider = new DataProtectorTokenProvider<AppUser> (dataProtectionProvider.Create("ASP.NET Identity")) { TokenLifespan = TimeSpan.FromDays(90d) }; 

在Seed方法期间,DataProtectorTokenProvider实例化是否会导致除null之外的任何内容,因为站点/应用程序没有运行/尚未启动,而且OWIN是如何工作的?感谢。

以下是PMC的输出:

运行种子方法。

System.NullReferenceException:未将对象引用设置为对象的实例。

在Identity \ AppUserManager.cs中的Identity.AppUserManager..ctor(IUserStore`1商店):第25行

在Migrations \ Configuration.cs中的Migrations.Configuration.Seed(EFDbContext上下文):第49行

at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)

at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()

at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()

at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)

at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)

at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)

at System.Data.Entity.Migrations.DbMigrator。&lt;&gt; c__DisplayClassc.b__b()

at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)

at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)

at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)

at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)

at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()

在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)

在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)

在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)

at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)

at System.Data.Entity.Migrations.UpdateDatabaseCommand。&lt;&gt; c__DisplayClass2。&lt; .ctor&gt; b__0()

at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

对象引用未设置为对象的实例。

以下是配置代码:

internal sealed class Configuration: DbMigrationsConfiguration<Website.Domain.Concrete.EFDbContext>
{       
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;

        ContextKey = "Website.Domain.Concrete.EFDbContext";
    }

    protected override void Seed(Concrete.EFDbContext context) 
    {         
        // This line is where we hit the exception
        AppUserManager userManager = new AppUserManager(new UserStore<AppUser>(context));

        AppRoleManager roleManager = new AppRoleManager(new RoleStore<AppRole>(context));

        ...

这是AppUserManager代码:

public class AppUserManager : UserManager<AppUser> 
{     
    public AppUserManager(IUserStore<AppUser> store) 
        : base(store) 
    {
        IDataProtectionProvider dataProtectionProvider = IdentityConfig.DataProtectionProvider; 

        // This is causing the NullReferenceException
        UserTokenProvider = new DataProtectorTokenProvider<AppUser>(dataProtectionProvider.Create("ASP.NET Identity")) { TokenLifespan = TimeSpan.FromDays(90d) }; 
    }

    public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
    {           
        EFDbContext db = context.Get<EFDbContext>();

        AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));

        manager.PasswordValidator = new PasswordValidator 
        {
            RequiredLength = 8,                 
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false             
        };

        manager.UserValidator = new UserValidator<AppUser>(manager) 
        {
            AllowOnlyAlphanumericUserNames = true,
            RequireUniqueEmail = true
        };

        return manager;
    }       
}

这是IdentityConfig文件。我有这个而不是一个启动文件,我告诉Owin在启动时使用此键在web.config中的appsettings中运行它

<add key="owin:AppStartup" value="Website.Domain.App_Start.IdentityConfig" />:

namespace Website.Domain.App_Start
{
    public class IdentityConfig 
    {    
        public static IDataProtectionProvider DataProtectionProvider { get; set; } 

        public void Configuration(IAppBuilder app)
        {
            DataProtectionProvider = app.GetDataProtectionProvider(); 

            app.CreatePerOwinContext<EFDbContext>(EFDbContext.Create);

            app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);

            app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create); 

            app.CreatePerOwinContext<AppSignInManager>(AppSignInManager.Create);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/account/login")
            });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

失败了因为

IDataProtectionProvider dataProtectionProvider = IdentityConfig.DataProtectionProvider;
除非应用程序实际运行,否则

将返回null。因此,在迁移期间失败。

以下是修复:

if (dataProtectionProvider != null)
{
    UserTokenProvider = new DataProtectorTokenProvider<AppUser>(dataProtectionProvider.Create("ASP.NET Identity")) { TokenLifespan = TimeSpan.FromDays(90d) }; 
}