【发布时间】:2015-10-28 21:45:10
【问题描述】:
首先,我使用 AspNet Identity Sample -pre 通过包管理器控制台安装,但 razor 文件不匹配,因此智能感知停止在视图页面上工作。
所以,我选择一步一步手动放置所有代码文件,并在遇到它们时解决了丢失的问题。 所以,我的 App_Start 文件夹中有一个 identityConfig.cs 文件,如下所示----
public class IdentityConfig
{
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
};
在我的帐户控制器中,我有这样的代码-------
public class AccountController : Controller
{
public AccountController()
{
}
public AccountController(IdentityConfig.ApplicationUserManager userManager, IdentityConfig.ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
private IdentityConfig.ApplicationUserManager _userManager;
public IdentityConfig.ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<IdentityConfig.ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
现在,我在注册操作方法中遇到错误-----
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
// await UserManager.SetTwoFactorEnabledAsync(user.Id, true);
if (result.Succeeded)
{
return await GenerateEmailConfirmation(user);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
在这里,如您所见,在第六行,它在第 1 行给出 System.NullReferenceException --------
var result = await UserManager.CreateAsync(user, model.Password);
我试图调试它,在我的本地窗口中,它正在传递正确填充数据库所需的所有内容,但只有这个结果变量是 null.also, _signInManager、userManager 和 SignInManager 和 USerManager 在本地窗口中都显示为 null。
有什么问题。我错过了什么。请告诉我。这个 UserMAnager 类有一些关系。我不知道是什么。
【问题讨论】:
标签: asp.net-mvc controller ef-code-first asp.net-identity