【问题标题】:Get return URL on Custom Authorize Attribute获取自定义授权属性的返回 URL
【发布时间】: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


【解决方案1】:

您需要在 get Action 上收到 returnUrl;

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

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

        @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
....
 }

你的代码的其余部分都很好

【讨论】:

    【解决方案2】:

    在您的 customeAuthorizer 属性中,您应该有 filterContext 对象,然后您可以使用以下代码示例。

            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            filterContext.HttpContext.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Please Provide authToken";
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary(
                    new
                    {
                        controller = "Authentication",
                        action = "Login",
                        errorMessage = "Invalid Resourse Access Attempt",
                        ReturnUrl = filterContext.HttpContext.Request.Path.Value
                    })); 
    

    或者您也可以为此目的使用以下函数。

    public void AuthFailed(AuthorizationFilterContext filterContext)
        { 
            Console.WriteLine(filterContext.HttpContext.Request.Path.Value);
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            filterContext.HttpContext.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Please Provide authToken";
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary(
                    new
                    {
                        controller = "Authentication",
                        action = "Login",
                        errorMessage = "Invalid Resourse Access Attempt",
                        ReturnUrl = filterContext.HttpContext.Request.Path.Value
                    })); 
        }
    

    在您的登录 (GET) 操作中,您可以像这样处理它。

    TempData["ReturnUrl"] = Request.Query["returnUrl"].ToString();
    

    在成功登录后(当用户成功登录时),您必须将其重定向到相同的请求页面。登录(POST)

                   if (TempData["ReturnUrl"] != null)
                    {
                        string[] temp = TempData["ReturnUrl"].ToString().Split('/');
                        if (temp.Length == 3)
                        {
                            return RedirectToAction(temp[1], temp[0], new { id = temp[2] });
                        }
                        else if (temp.Length == 1)
                        {
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            return RedirectToAction(temp[1], temp[0]);
                        }
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多