即使在成功登录后,它也不会重定向到操作,而是停留在登录页面上。
我设置了断点,但一直不知道我的代码到底出了什么问题。
即使条件为true,代码也不会执行。
这是我的代码:
[HttpPost]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == model.UserName);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user, model.Password, true, false);
if (result.Succeeded)
{
var role = await _userManager.GetRolesAsync(user);
if (role.Contains("Client"))
{
return RedirectToAction("Index", "MyDashboard");
}
if (role.Contains("Admin"))
{
return RedirectToAction("Index", "Admin");
}
}
}
else
{
ModelState.AddModelError("", "Invalid login attempt");
return View(model);
}
}
return View();
}
我自己得到了解决方案。实际上,我在启动类中缺少身份验证中间件。添加应用程序后。使用Authentication((,它运行良好。谢谢