当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - Too many redirect when redirecting if authenticated MVC 5

问题描述:

I wanted to escape login page if user is already authenticated so i follow the suggestion provided by this question. Then i got this problem of too many redirect.I got this error when i tried to login for the very first time even though i logged in successfully. Apart from the first time it worked as charm.I am new to MVC . I searched all over and found some quite same scenario but they didn't work for me so here i am asking for your help . Thanks in advance .

This is my main page after user successfully logged in.(Home controller)

 [Authorize]

public ActionResult Main()

{

return View();

}

This is my login controller action .

 [AllowAnonymous]

public ActionResult Login(string returnUrl)

{

ViewBag.ReturnUrl = returnUrl;

if (Request.IsAuthenticated)

{

return RedirectToAction("Main", "Home");

}

return View();

}

I am using form authentication for login .

web.config

 <system.web>

<compilation debug="true" targetFramework="4.6" />

<httpRuntime targetFramework="4.6" />

<authentication mode="Forms">

<forms loginUrl="~/" defaultUrl="~/Login/Login">

</forms>

</authentication>

</system.web>

here is screenshot

网友答案:

I think the problem is that when you redirect to your home page it's taking you back to the login page because you have set the defaultUrl to be your login page.

I would suggest that you set defaultUrl to "~/Home/Main" and see if that helps. Also, you probably want loginUrl to be "~/Login/Login" or something

分享给朋友:
您可能感兴趣的文章:
随机阅读: