我正在使用带有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>)'
请注意,我已经在数据库中创建了新表,用于迁移旧成员资格和角色以用于身份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控制器已创建,但未实现任何方法。需要这个控制器吗?或者,我可以删除它并引用它吗?
答案 0 :(得分:1)
您的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
。确保你的课程符合你的要求。