[英]ReturnUrl Points to an ActionResult
这是场景的方式:
TestController
的Index
方法的链接 TestController
上的ActionMethod的链接 Index
操作方法 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;
}
<!DOCTYPE html>
<html lang="en">
<head>
.....................
</head>
<body>
<div class="container">
@using (@Html.BeginForm("Login", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post, new { @class = "form-signin" }))
{
@Html.AntiForgeryToken()
....................
....................
}
</div>
</body>
</html>
Web配置
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="1" />
</authentication>
返回网址的期望是 :
相反,当前值是 :
备注 :
这是你提到的正常行为!
MVC框架将用户重定向到Login页面,并将ActionMethod名称附加到URL,而不是附加
Index
操作方法
非常感谢MVC Security管道。 当您使用表单身份验证并且用户未经过身份验证或授权时,ASP.NET安全管道会重定向到登录页面并将returnUrl
作为参数传递给等于重定向到登录页面的页面 (这是需要授权的控制器操作)您通过单击链接调用的)。
所以在这里你不能指望索引 (当前加载的页面没有有效和持久的身份验证),随后ActionMethod
调用安全管道,并且会returnurl
枚举returnurl
。
请注意,这是因为Controller和View之间的同步通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.