获取自定义授权属性的返回URL

时间:2015-09-18 08:40:48

标签: c# asp.net-mvc asp.net-mvc-5

我有自定义Authorize属性来处理LogIn。我需要在登录后将用户重定向到最后一页。例如:

产品控制器

[CustomAuthorize]
public ActionResult Detail(int productID)
{
     //code here
     return View(model);
}

假设用户在尝试访问Product/Detail/msi-gtx-970时未登录,我的网络应用会将用户重定向到LogIn页面。我想在成功Product/Detail/msi-gtx-970后将用户重定向回LogIn。怎么做?

我的LogIn控制器

[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);
}

由于

1 个答案:

答案 0 :(得分:0)

您需要在get Action上收到returnUrl;

getView()

在“登录”视图中更改您的表单,将url作为参数发送,以发布网址值:

       [AllowAnonymous]
       public ActionResult Login(string returnUrl)
       {
           ViewBag.ReturnUrl = returnUrl;
           return View();
       }

你的其余代码很好