在aspnet Identity中配置applicationUserManager和角色管理器的位置

时间:2015-10-28 21:45:10

标签: asp.net-mvc controller ef-code-first asp.net-identity

首先,我使用 AspNet Identity Sample -pre通过软件包管理器控制台安装,但是razor文件不匹配,因此intellisense停止在视图页面上工作。

所以,我选择手动将所有代码文件一步一步地放入并解决了因为它们以我的方式丢失的问题。 所以,我在我的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;
        }
    }

现在,我在Register Action方法中遇到错误------

      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都在本地窗口中显示为空。

问题是什么。我错过了什么请告诉我。这个UserMAnager类有什么关系。我不知道是什么。

1 个答案:

答案 0 :(得分: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);

        ...
    }
}