c# ArgumentNullException:值不能为空。 (参数'值')System.ArgumentNullException.Throw(字符串paramName)

问题描述 投票:0回答:1

注册过程没有问题,但无法登录。 它在错误详细信息中显示这一行:

var loginResult = await _signInManager.PasswordSignInAsync(doctor, loginViewModel.Password, true, false);

这是我的代码:

namespace JumpHospitalApp.MVC.Controllers
{
    public class AuthDoctorController : Controller
    {
        private readonly UserManager<HumanBase> _userManager;
        private readonly SignInManager<HumanBase> _signInManager;
        private readonly IToastNotification _toastNotification;
        private readonly IWebHostEnvironment _environment;
        public AuthDoctorController (UserManager<HumanBase> userManager, IToastNotification toastNotification, SignInManager<HumanBase> signInManager, IWebHostEnvironment environment)
        {
            _userManager = userManager;
            _toastNotification = toastNotification;
            _signInManager = signInManager;
            _userManager = userManager;
            _toastNotification = toastNotification;
            _environment = environment;
        }

        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        public IActionResult Register()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction(nameof(Index), "Home");
            }

            var registerViewModel = new AuthRegisterDoctorViewModel();


            return View(registerViewModel);
        }

        [HttpPost]
        public async Task<IActionResult> RegisterAsync(AuthRegisterDoctorViewModel registerViewModel)
        {
            if (!ModelState.IsValid)
                return View(registerViewModel);

            var doctorId = Guid.NewGuid();

            var doctor = new Doctor()
            {
                Id = doctorId,
                Email = registerViewModel.Email,
                UserName = registerViewModel.Email,
                FirstName = registerViewModel.FirstName,
                LastName = registerViewModel.LastName,
                Gender = registerViewModel.Gender,
                DateOfBirth = registerViewModel.BirthDate.Value.ToUniversalTime(),
                Password = registerViewModel.Password,
                CreatedOn = DateTimeOffset.UtcNow,
                CreatedByUserId = doctorId.ToString(),
                IsDeleted = false,
                
            };

            var identityResult = await _userManager.CreateAsync(doctor, registerViewModel.Password);

            if (!identityResult.Succeeded) 
            {
                foreach (var error in identityResult.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }

                return View(registerViewModel);
            }

            _toastNotification.AddSuccessToastMessage("You've successfully registered to the application. Letssgooooo:)");

      

            return RedirectToAction(nameof(Login));
        }
        [HttpGet]
        public IActionResult Login()
        {    
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }

            var loginViewModel = new AuthLoginDoctorViewModel();

            return View(loginViewModel);
        }
        [HttpPost]
        public async Task<IActionResult> LoginAsync(AuthLoginDoctorViewModel loginViewModel)
        {
            if (!ModelState.IsValid)
                return View(loginViewModel);

            if (string.IsNullOrEmpty(loginViewModel.Email) || string.IsNullOrEmpty(loginViewModel.Password))
            {
                _toastNotification.AddErrorToastMessage("Email and password are required.");
                return View(loginViewModel);
            }

            var doctor = await _userManager.FindByNameAsync(loginViewModel.Email);

            if (doctor is null)
            {
                _toastNotification.AddErrorToastMessage("Your email or password is incorrect.");

                return View(loginViewModel);
            }


            var loginResult = await _signInManager.PasswordSignInAsync(doctor, loginViewModel.Password, true, false);

            

            if (!loginResult.Succeeded)
            {
                _toastNotification.AddErrorToastMessage("Your email or password is incorrect.");

                return View(loginViewModel);
            }

            _toastNotification.AddSuccessToastMessage($"Welcome {doctor.FirstName}!");

            return RedirectToAction("Index", controllerName: "AuthDoctor");
        }
    }
}

注册过程没有问题,但无法登录。

c# asp.net-identity argumentnullexception
1个回答
0
投票

根据您的代码,看起来唯一的可能性是医生或密码是一个非空、非空字符串,纯粹由空格组成,该字符串被视为相当于 null。将 LoginAsync 中的 string.IsNullOrEmpy() 调用更改为 string.IsNullOrWhiteSpace()。无论如何,这是一种更安全的方法。

© www.soinside.com 2019 - 2024. All rights reserved.