这是场景的方式:
从头开始创建MVC项目
Test Controller使用[Authorize]属性修饰
用户登录并定向到主页
用户单击重定向到 TestController 的 Index 方法的链接
用户等待60秒以使表单身份验证超时
用户单击调用驻留在 TestController 上的ActionMethod的链接
MVC框架将用户重定向到Login页面并将ActionMethod名称附加到URL而不是附加 Index Action方法
TestController :
[Authorize]
public class TestController : Controller
{
// GET: Test
public ViewResult Index()
{
return View();
}
[ValidateInput(false)]
public ActionResult ActionTest()
{
return new EmptyResult();
}
}
HomeController :
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
AccountController :
public class AccountController : Controller
{
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
try
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
return RedirectToAction(controllerName: "Home", actionName: "Index");
}
catch
{
return View(model);
}
}
return View(model);
}
}
Login.chtml
@model TestLoginProject.Models.LoginViewModel
@{
Layout = null;
}
.....................
@using (@Html.BeginForm("Login", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post, new { @class = "form-signin" }))
{
@Html.AntiForgeryToken()
....................
....................
}
Web Config
The expectation of the return url is :
http:// localhost:2441 /帐户/登录?ReturnUrl =%2fTest%2fIndex
Instead, the current value is :
http:// localhost:2441 /帐户/登录?ReturnUrl =%2fTest%2fActionTest
Notes :
当用户在超时后单击链接时,在重定向到“登录”页面之前不会执行任何“测试操作”
所有路由都是在VS2017中从头开始执行Empty MVC项目时提供的默认路由