2015-10-28 18 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

相關問題