UserManager 参数类型不在范围内

UserManager Argument Type not within bounds

我正在使用带 EF6 的 MVC5 重写一个 MVC3 应用程序,并尝试将成员资格和角色 API 迁移到身份 2。我遵循了几个指南,但现在收到我需要的构建错误协助。

我的AccountController部分如下:

[Authorize]
public class AccountController : Controller
{
    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }

    public AccountController(UserManager<ApplicationUser> userManager)
    {
        UserManager = userManager;
    }

    public UserManager<ApplicationUser> UserManager { get; private set; }

我的UserManager如下:

public class UserManager : UserManager<User>
{
        public UserManager()
            : base(new UserStore<User>(new ApplicationDbContext()))
        {
            this.PasswordHasher = new SQLPasswordHasher();
        }
}

我的ApplicationDbContext如下:

public class ApplicationDbContext : IdentityDbContext<User>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
        this.Database.Log = Logger;
    }

    private void Logger(string log)
    {
        Debug.WriteLine(log);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        var application = modelBuilder.Entity<Application>();
        application.HasKey(t => t.ApplicationId).ToTable("Applications");

        modelBuilder.Entity<UserDbProfile>().ToTable("Profiles");

    }

    public virtual IDbSet<Application> Applications { get; set; }
    public virtual IDbSet<UserDbProfile> Profiles { get; set; }
}

我的用户模型如下:

public class User : IdentityUser
{
    public User()
    {
        CreateDate = DateTime.UtcNow;
        IsApproved = false;
        LastLoginDate = DateTime.UtcNow;
        LastActivityDate = DateTime.UtcNow;
        LastPasswordChangedDate = DateTime.UtcNow;
        LastLockoutDate = MinSqlDate;
        FailedPasswordAnswerAttemptWindowStart = MinSqlDate;
        FailedPasswordAttemptWindowStart = MinSqlDate;
        Profile = new ProfileInfo();
    }

    public System.Guid ApplicationId { get; set; }
    public bool IsAnonymous { get; set; }
    public System.DateTime? LastActivityDate { get; set; }
    public string Email { get; set; }
    public string PasswordQuestion { get; set; }
    public string PasswordAnswer { get; set; }
    public bool IsApproved { get; set; }
    public bool IsLockedOut { get; set; }
    public System.DateTime? CreateDate { get; set; }
    public System.DateTime? LastLoginDate { get; set; }
    public System.DateTime? LastPasswordChangedDate { get; set; }
    public System.DateTime? LastLockoutDate { get; set; }
    public int FailedPasswordAttemptCount { get; set; }
    public System.DateTime? FailedPasswordAttemptWindowStart { get; set;}
    public int FailedPasswordAnswerAttemptCount { get; set; }
    public System.DateTime? FailedPasswordAnswerAttemptWindowStart 
    { get; set; }
    public string Comment { get; set; }

    public ProfileInfo Profile { get; set; }

    private static readonly DateTime MinSqlDate = 
    DateTime.Parse("1/1/1754");
}

收到的具体错误类似于:

Error   16  
Inconsistent accessibility: parameter type
'Microsoft.AspNet.Identity.UserManager' is less accessible than method 
'Controllers.AccountController.AccountController(Microsoft.AspNet.
Identity.UserManager<Controllers.ApplicationUser>)' 

请注意,我已经在数据库中创建了新表,这些表迁移了旧的成员资格和角色以用于 Identity 2。

必须如何解决错误并确保新的身份 2 方法正常工作?

更新 我的 AccountController 代码现在如下:

public AccountController()
    : this(new UserManager(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {

    }

UserManager如下,部分:

public class UserManager : UserManager<User> 
{
    private UserStore<Controllers.ApplicationUser> userStore;

        public UserManager()
            : base(new UserStore<User>(new ApplicationDbContext()))
        {
            this.PasswordHasher = new SQLPasswordHasher();
        }


}

构建错误状态:

Error   19  'UserManager' does not contain a constructor that takes 1 arguments 

请注意,已创建 ApplicationUser 控制器,但未实现任何方法。需要这个控制器吗?或者,我可以删除它和对它的引用吗?

您的 AccountController 需要使用您的 UserManager,而不是 UserManager<ApplicationUser>,后者是框架的一部分:

[Authorize]
public class AccountController : Controller
{
    public AccountController()
        : this(new UserManager(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }

    public AccountController(UserManager userManager)
    {
        UserManager = userManager;
    }

    public UserManager UserManager { get; private set; }

我还注意到在控制器中你有 ApplicationUser,但你的用户对象实际上是 User。确保您与 类.

一致