簡體   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