为什么 result.Succeeded return 在 ASP 身份中失败

Why result.Succeeded return failed in ASP Identity

当尝试添加在注册过程中添加姓名的功能时,登录帐户时出错。我注册的时候登录成功了,但是一退出再尝试登录,就提示登录错误

对日志文件所做的更改

            var user = CreateUser();
            await _userStore.SetUserNameAsync(user, Input.UserName, CancellationToken.None);
            await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
            var result = await _userManager.CreateAsync(user, Input.Password);

这里UserNameAsync中email的标准值,我会改成用户名

部分when where错误

            if (ModelState.IsValid)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true
            var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
            if (result.Succeeded) //Return failed
            {
                _logger.LogInformation("User logged in.");
                return LocalRedirect(returnUrl);
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return Page();
            }
        }

您可以检查 EmailConfirmed Colum 是否在您的数据库中有值, 如果您使用 e-mail 注册,e-mail 将发送给您至 comfirm.and,您的错误可能由此引起。

您也可以在启动时尝试如下设置class以避免错误

services.Configure<IdentityOptions>(options =>
            {
                ....
                options.SignIn.RequireConfirmedAccount = false;
                ....
             });