CreateIdentityAsync,RavenDb.AspNet.Identity。异常:未找到 UserId

CreateIdentityAsync, RavenDb.AspNet.Identity. Exception: UserId not found

我看过这个主题中的其他问题,但似乎我遗漏了一些我无法从其他问题中找到的东西。可能跟RavenDb.AspNet.Identity有关。无论如何,我看不出我的代码哪里做错了。

当我注册用户时。将 IdentityUserByUserNameApplicationUser 保存到 RavenDb 一切顺利。但是当它转到 await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); 处的用户登录时,出现了问题。并抛出错误:"UserId not found".

    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
           // Note the authenticationType must match the one defined in   CookieAuthenticationOptions.AuthenticationType
           var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
           // Add custom user claims here

           return userIdentity;
        }
    }

        //from AccountController start
        [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Id = "RavenDbUsers/" + model.Email };

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

            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
    //from AccountController end


    public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
    {
       public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager) 
       {
       }

       public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
       {
          return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
       }

       public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
       {
          return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
      }
  }

更新:

//controller的ctor、SingInManager、UserManager

        private ApplicationSignInManager _signInManager;
    private UserManager<ApplicationUser> _userManager;

    public AccountController()
    {
        _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(() => AsyncDataSession));
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

    public UserManager<ApplicationUser> UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

//更新:ApplicationUserManager

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var session = context.Get<IAsyncDocumentSession>();
        session.Advanced.UseOptimisticConcurrency = true;

        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(session));
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        manager.UserLockoutEnabledByDefault = true;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
        {
            MessageFormat = "Your security code is {0}"
        });
        manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
        {
            Subject = "Security Code",
            BodyFormat = "Your security code is {0}"
        });
        manager.EmailService = new EmailService();
        manager.SmsService = new SmsService();
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider =
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }

RavenDB.AspNet.Identity 的作者在这里。

当你到达这一行时,user.Id的值是多少?

await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

此外,您能否在执行该代码之前验证用户是否确实存在于数据库中?

我的心理调试技巧告诉我,您的 UserManager 可能使用与 accountController.AsyncDataSession 不同的 IAsyncDocumentSession,因此,当您进行 SignInAsync 调用时,用户实际上并不在数据库中。