简体   繁体   中英

ReturnUrl is null in ASP.NET Core login

I have ASP.NET Core application with individual accounts; very similar to what gets generated by VS2017. For testing purposes I put [Authorize] attribute on About() action in Home controller. I am redirected to Login page as expected, and I see that URL is http://localhost:5000/Account/Login?ReturnUrl=%2FHome%2FAbout - also as expected. However, in the POST Login method ReturnUrl is null. I have Login method in Account Controller:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginInputModel model) {
...
}

I also tried ReturnUrl as parameter explicitly, with or without [FromQuery] . In all permutations it is null.

This is how i managed to get mine working

The Get Action

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                if (Url.IsLocalUrl(ViewBag.ReturnUrl))
                    return Redirect(ViewBag.ReturnUrl);

                return RedirectToAction("Index", "Home");
            }

            return View();
        }

My Form is like this :

  <form asp-action="Login" method="post" asp-route-returnurl="@ViewBag.ReturnUrl" >

The post action :

[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Login(VMLogin model, string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;

    if (!ModelState.IsValid)
    {
        return View(model);
    }

     //Authentication here 
  
    if (Url.IsLocalUrl(ViewBag.ReturnUrl))
        return Redirect(ViewBag.ReturnUrl);

 
    return RedirectToAction("Index", "Home");
}

You should be sure that you are using

Html.BeginForm("Login", "Account", new {ReturnUrl = Request.QueryString["ReturnUrl"] })


[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginInputModel model, string ReturnUrl) {
...
}

For .net core this is how you can fix the issue.

In your View,

@using (Html.BeginForm(new { returnUrl = Context.Request.Query["ReturnUrl"] }))

In your Controller,

[HttpPost]
public IActionResult Login(YourViewModel m, string returnUrl = null)
{
 if (!string.IsNullOrEmpty(returnUrl))
 {
  return LocalRedirect(returnUrl);
 }
 return RedirectToAction("Index", "Home");
}

first you must get return url in get method like this :

[HttpGet]
public IActionResult Login(string returnUrl)
{
    TempData["ReturnUrl"] = returnUrl;
    return View();
}

get returnUrl as parameter in get method and send in to post method by tempdata.

the post method also like this :

[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
    //Your Login Code ...

    if (!string.IsNullOrEmpty(TempData["ReturnUrl"] as string) && Url.IsLocalUrl(TempData["ReturnUrl"] as string))
    {
        return Redirect(TempData["ReturnUrl"] as string);
    }

    return RedirectToAction(controllerName:"Home",actionName:"Index");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM