1

我正在尝试在 aspnet 样板中使用身份实现忘记密码。

  1. 我在 Visual Studio 中创建了一个具有个人用户身份验证的新项目,并实现并测试了忘记密码功能(项目名称:)IdentityProject
  2. 然后我下载了 abp 项目并将App_Start/AppIdentityConfig.csandIdentityModels文件从复制IdentityProjectABPProject.
  3. Startup.cs在方法的类中写下这一行Configurationapp.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方法的第一行遇到异常:

值不能为空。参数名称:上下文

Microsoft.AspNet.Identity.EntityFramework.dll 中出现“System.ArgumentNullException”类型的异常,但未在用户代码中处理

虽然 context 参数具有以下数据:

以下是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)
    {

    }
}

这个异常的原因可能是什么以及如何解决这个问题?

4

1 回答 1

0

在您的代码context.Get<ApplicationDbContext>()中返回 null(或看起来为 null)。

在您的 startup.cs 中,您需要添加

app.CreatePerOwinContext<ApplicationDbContext>(ApplicationDbContext.Create);
于 2017-08-10T22:35:36.100 回答