如何在 asp.Net MVC 5 中进行身份验证

How to do authentication in asp.Net MVC 5

我有 HomeController,其索引方法有 [Authorize],所以如果没有登录,它会重定向到登录操作,成功登录后,它会重定向到 Patient Home controller.But问题是身份验证不起作用。

    [Authorize]
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(User user)
    {
       User usr = _logingService.Login(user.Email,user.Password);
        if(usr!=null)
        {
            Patient pat = _patientService.GetPatient(usr.Id);
            FormsAuthentication.SetAuthCookie(pat.Name, false);
            if (usr.Role == "Patient")
            {

                return RedirectToAction("Index", "Home", new { Area = "Patient" });
            }
            else
            {
            }

        }

        return View();
    }

现在我正在从数据库中获取实际值,但是在重定向到患者索引操作之后,我再次使用了 [Authorize] 但它再次重定向到登录页面

    [Authorize]
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

从 web.config 删除:

<modules>
    <!--<remove name="FormsAuthenticationModule" />-->
</modules>

或 simples 删除 web.config 中的行,它将正常工作。