【问题标题】:UserTokenProvider/DataProtectorTokenProvider NullReferenceException during Code First Migrations Seed method代码优先迁移种子方法期间的 UserTokenProvider/DataProtectorTokenProvider NullReferenceException
【发布时间】:2016-02-09 17:13:32
【问题描述】:

架构更新正常,但种子方法由于 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..ctor(IUserStore`1 store) in Identity\AppUserManager.cs:line 25

在 Migrations\Configuration.cs:line 49 中的 Migrations.Configuration.Seed(EFDbContext context)

在 System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext 上下文)

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

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

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

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

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

在 System.Data.Entity.Migrations.DbMigrator.c__DisplayClassc.b__b()

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

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

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

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

在 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)

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

在 System.Data.Entity.Migrations.UpdateDatabaseCommand.c__DisplayClass2.<.ctor>b__0()

在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令)

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

这里是配置代码:

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")
            });
        }
    }
}

【问题讨论】:

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


【解决方案1】:

失败是因为

IDataProtectionProvider dataProtectionProvider = IdentityConfig.DataProtectionProvider;

除非应用程序实际运行,否则将返回 null。因此,这在迁移期间失败。

解决方法如下:

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

【讨论】:

    猜你喜欢
    • 2015-04-29
    • 2015-05-05
    • 2016-05-26
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多