【发布时间】:2015-04-10 13:07:09
【问题描述】:
我使用 ASP.NET Identity 创建了一个新的 MVC5 应用程序。我没有更改很多文件。这是我得到的错误:
项目中的以下更改与 VisualStudio 模板不同:
ApplicationUserManager.cs
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> userStore)
: base(userStore)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
// Use DI to resolve the UserStore
var userStore = (IUserStore<User>) DependencyResolver.Current.GetService(typeof (IUserStore<User>));
var manager = new ApplicationUserManager(userStore);
// rest is same as in the template ...
}
}
我的 DependencyResolver 是 Ninject。我为它创建了以下绑定:
Bind<IMyDbContextFactory>().To<MyDbContextFactory>().InSingletonScope();
Bind<IUserStore<User>>().To<UserStore>().WithConstructorArgument("factory", factory => Kernel.Get<MyDbContextFactory>());
UserStore.cs
public class UserStore : IUserStore<User>,
IUserLockoutStore<User, string>,
IUserPasswordStore<User>,
IUserTwoFactorStore<User, string>,
IUserEmailStore<User>
{
private readonly IMyDbContextFactory _factory;
public UserStore(IMyDbContextFactory factory)
{
_factory = factory;
}
public void Dispose()
{
// Nothing to dispose here
}
public Task CreateAsync(User user)
{
using (var context = _factory.CreateContext())
{
// TODO: Validation
context.Users.Add(user);
var result = context.SaveChanges();
return Task.FromResult(result);
}
}
// the rest of the implementend methods from the interfaces ...
}
为了完整起见,用户实体、DbContext 和我用来在UserStore.cs 中检索 DbContext 的工厂
用户.cs
public class User : IUser
{
public string Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public object PhoneNumber { get; set; }
public int AccessFailedCount { get; set; }
public bool LockoutEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool TwoFactorEnabled { get; set; }
public virtual ICollection<UserRole> ApplicationUserRoles { get; set; }
public virtual ICollection<UserLogin> ApplicationUserLogins { get; set; }
}
MyDbContextFactory.cs
public class MyDbContextFactory : IMyDbContextFactory
{
public IMyDbContext CreateContext()
{
return new MyDbContext();
}
}
MyDbContext.cs
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext()
: base("MyDbContextConnection")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
Database.Initialize(true);
}
// DbSets, OnModelCreating and other stuff ...
}
异常意味着 UserManager 为空。但事实并非如此:
看来ApplicationUserManager 并没有真正使用(或设置)我注入的UserStore。但这是为什么呢?
【问题讨论】:
-
如何将
UserManager输入控制器?默认情况下,它是从OwinContext中提取的,而不是从您的 DI 中提取的。在您的异常消息中,UserManager似乎为空。 -
UserManager 仍然是从
OwinContext中提取的。我没有更改应用程序的那部分。我仍然在Startup.Auth.cs中使用app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);。不,UserManager 不为空。 (我在我的问题中添加了截图) -
因此,如果您逐步执行 `ApplicationUserManager.Create` 方法。您是否收到从
DependencyResolver返回给您的UserStore?DependencyResolver是否解决了任何其他对象? -
DependencyResolver 可以正常工作。我也尝试直接使用具体类但没有成功。这不是问题。与此同时,我发现了问题所在。我现在正在发布答案。感谢您的帮助。
标签: asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2