繁体   English   中英

如何在ASP.NET MVC 5中进行身份验证

[英]How to do authentication in asp.Net MVC 5

我在HomeController上的索引方法具有[Authorize],因此如果未登录,它将重定向到登录操作,并在成功登录后将其重定向到Patient Home控制器。但是问题是身份验证无法正常工作。

    [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]
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

从web.config中删除:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM