【发布时间】:2015-09-18 08:40:48
【问题描述】:
我有一个自定义的Authorize 属性来处理LogIn。我需要在登录后将用户重定向到最后一页。例如:
产品控制器
[CustomAuthorize]
public ActionResult Detail(int productID)
{
//code here
return View(model);
}
假设用户在尝试访问Product/Detail/msi-gtx-970 时没有登录,我的Web 应用程序会将用户重定向到LogIn 页面。我想在成功LogIn 后将用户重定向回Product/Detail/msi-gtx-970。该怎么做?
我的登录控制器
[AllowAnonymous]
public ActionResult LogIn()
{
//code here
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult LogIn(string returnUrl)
{
//code here
if (string.IsNullOrEmpty(returnUrl))
{
return View("Index", "Home");
}
return Redirect(returnUrl);
}
谢谢
【问题讨论】:
-
默认情况下会发生这种情况。在 VS 中创建新应用时查看 AccountController 中的代码(它使用 returnUrl 参数)
-
@StephenMuecke:我检查了
AccountController中的默认模板LogIn方法,它在GET 方法中有string returnUrl,所以我尝试在我的GET 方法中添加public ActionResult LogIn(string returnUrl),但它始终为null aka不工作。 -
你需要研究所有相关的代码,看看它是如何工作的
-
@StephenMuecke:问题是 ASP 身份假设每次用户点击登录页面时都会创建一个
QueryString["returnUrl"],例如:localhost:12345/Account/LogIn?ReturnUrl=%2FCart但即使它来自@987654335,我也没有得到任何东西@ 要求。知道为什么吗?
标签: c# asp.net-mvc asp.net-mvc-5