【问题标题】:UserManager Argument Type not within boundsUserManager 参数类型不在范围内
【发布时间】:2015-03-22 20:13:12
【问题描述】:

我正在使用带有 EF6 的 MVC5 重写一个 MVC3 应用程序,并尝试将成员资格和角色 API 迁移到 Identity 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。

必须做些什么来解决错误并确保新的 Identity 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。感谢您的帮助。

标签: asp.net-mvc entity-framework asp.net-identity


【解决方案1】:

您的 AccountController 需要使用您的 UserManager,而不是作为框架一部分的 UserManager&lt;ApplicationUser&gt;

[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。确保您与您的课程保持一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2021-07-17
    • 2018-01-22
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多