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

本文关键字:返回 URL 属性 自定义 授权 获取 | 更新日期: 2023-09-27 18:13:30

我有一个自定义的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。怎么做呢?

My LogIn Controller

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

谢谢

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

您需要在get Action上接收returnUrl;

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

修改"Login"视图上的表单,传递url作为发送url值的参数:

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

剩下的代码就可以了

在您的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");
                }