2017-08-10 70 views
1

我試圖在aspnet樣板文件中使用Identity實現忘記密碼。在aspnet樣板文件中實現忘記密碼

  1. 我與個別用戶身份驗證Visual Studio創建一個新項目,實施和測試忘記密碼的功能(項目名稱:IdentityProject)。
  2. 然後我下載了abp項目並將App_Start/AppIdentityConfig.csIdentityModels文件從IdentityProject複製到ABPProject
  3. Startup.cs類此行Configuration方法:app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

下面是我ApplicationUserManagerIdentityConfig.cs

public class ApplicationUserManager : UserManager<ApplicationUser> 
{ 
    public ApplicationUserManager(IUserStore<ApplicationUser> store) 
     : base(store) 
    { 
    } 

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
     // Configure validation logic for usernames 
     manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 

     // Configure validation logic for passwords 
     manager.PasswordValidator = new PasswordValidator 
     { 
      RequiredLength = 6, 
     }; 

     //removed some of the configuration code.. 

     var dataProtectionProvider = options.DataProtectionProvider; 
     if (dataProtectionProvider != null) 
     { 
      manager.UserTokenProvider = 
       new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
     } 
     return manager; 
    } 
} 

在運行的應用程序,我對Create方法的第一行越來越例外:

值不能爲空。參數名:上下文

類型「System.ArgumentNullException」發生在 Microsoft.AspNet.Identity.EntityFramework.dll但儘管上下文參數有這個數據是不是在 用戶代碼

處理的例外:

下面是ApplicationDbContext類身份:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 

下面是類的ABPProjectDBContext

public class HRISDbContext : AbpZeroDbContext<Tenant, Role, User> 
{ 

    public HRISDbContext() 
     : base("Default") 
    { 

    } 
    public HRISDbContext(string nameOrConnectionString) 
     : base(nameOrConnectionString) 
    { 

    } 

    //This constructor is used in tests 
    public HRISDbContext(DbConnection existingConnection) 
    : base(existingConnection, false) 
    { 

    } 

    public HRISDbContext(DbConnection existingConnection, bool contextOwnsConnection) 
    : base(existingConnection, contextOwnsConnection) 
    { 

    } 
} 

可能是什麼這個異常的原因,以及如何解決這個問題?

+0

請讓我知道,如果我的問題不明確。 –

回答

0

在您的代碼context.Get<ApplicationDbContext>()返回null(或看起來像它爲null)。

在你startup.cs你需要添加

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