[英]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.