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



首先,我使用了AspNet Identity Sample-通过包管理器控制台预装,但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,_signInManager、userManager和signInManager以及userManager都在locals窗口中显示为null。

问题出在哪里。我所缺少的。请告诉我,这与UserMAnager类有关。我不知道是什么。

确保在启动类中创建了它们

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);
        ...
    }
}

相关内容

  • 没有找到相关文章

最新更新