我在登录过程中使用以下代码。我将断点保留在登录控制器和服务层的validateUser函数中,以检查哪里出错了,发现在ValidateUser函数中dbUser对象中获取了空值服务层,它显示无效的登录尝试。我哪里做错了?我想念什么吗?
控制器视图
public ActionResult Login(UserLoginDTO userLoginDTO)
{
try
{
if (ModelState.IsValid)
{
userLoginDTO.Password = Helper.EncodePasswordToBase64(userLoginDTO.Password);
UserLoginDTO user = userLoginService.ValidateUser(userLoginDTO);
if (user != null)
{
this.SignInUser(user, false);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid login attempt.");
}
}
catch (Exception ex)
{
Log.Error(ex);
//throw;
}
return View(userLoginDTO);
}
模型视图
public class UserLoginDTO
{
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
服务层
public interface IUserLoginService
{
UserLoginDTO ValidateUser(UserLoginDTO userLoginModel);
}
public class UserLoginService : IUserLoginService
{
private IUnitOfWork unitOfWork;
public UserLoginService(IUnitOfWork _unitOfWork)
{
unitOfWork = _unitOfWork ?? new UnitOfWork();
}
public UserLoginDTO ValidateUser(UserLoginDTO userLoginModel)
{
UserLogin dbUser = unitOfWork.UserLoginRepository.All()
.Where(x => x.UserName == userLoginModel.UserName
&& x.Password == userLoginModel.Password)
.FirstOrDefault();
if (dbUser != null)
{
UserLoginDTO userDTO = dbUser.Convert();
return userDTO;
}
return null;
}
}
存储层
public interface IUnitOfWork
{
int Save();
Task<int> SaveAsync();
IRepository<UserLogin> UserLoginRepository { get; }
}
private IRepository<UserLogin> _userLoginRepository;
public IRepository<UserLogin> UserLoginRepository
{
get
{
return _userLoginRepository ??
(_userLoginRepository = new RepositoryBase<UserLogin>(_context));
}
}