2015-10-28 19 views
1

首先,我已经使用AspNet Identity Sample -pre通过包管理器控制台安装,但是在剃须刀文件中存在不匹配,因此intellisense在视图页面上停止工作。在哪里配置aspnet中的applicationUserManager和角色管理器身份

所以,我选择手动把所有的代码文件一步一步解决使用,因为他们来我的方式。 所以,我有这样我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); 
    } 

这里,就像你看到的,在第六行,它是在给线---- System.NullReferenceException ---

var result = await UserManager.CreateAsync(user, model.Password); 

我试图调试它,在我的本地窗口,它传递给正确填充数据库所需的每一件事情,但只有这样的结果变量是null.also, _signInManager,和的UserManager和SignInManager的UserManager全部都出现了在当地人窗口中为null。

问题是什么。我错过了什么。请告诉我。这个UserMAnager类有一些事情要做。我不知道该怎么做。

回答

0

确保你让他们在你的启动类创建:

public partial class Startup 
{ 
    public void ConfigureAuth(IAppBuilder app) 
    { 
     // Configure the db context, user manager and signin manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

     ... 
    } 
} 
+0

Thnxx的帮助其工作就像一个魅力thnkkü非常非常much.Plzz解释我为什么需要在这里配置。在aspnet身份示例中,不需要像这样配置它。 – duke

+0

return _userManager? 。HttpContext.GetOwinContext()GetUserManager (); ...你从你的OwinContext获得你的用户管理器,所以在应用程序的启动时,你必须确保它会在那里... –

+0

我有以下问题的问题可以看看这个,并建议我该怎么做我做http://stackoverflow.com/questions/33455346/userid-not-found-error-in-aspnet-identity-at-generateuseridentityasync-method?noredirect=1#comment54697266_33455346 – duke

相关问题