简体   繁体   English

ASP.NET Core 登录中的 ReturnUrl 为空

[英]ReturnUrl is null in ASP.NET Core login

I have ASP.NET Core application with individual accounts;我有个人帐户的 ASP.NET Core 应用程序; very similar to what gets generated by VS2017.与 VS2017 生成的非常相似。 For testing purposes I put [Authorize] attribute on About() action in Home controller.出于测试目的,我将[Authorize]属性放在 Home 控制器中的About()操作上。 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.我按预期重定向到登录页面,我看到 URL 是http://localhost:5000/Account/Login?ReturnUrl=%2FHome%2FAbout - 也是预期的。 However, in the POST Login method ReturnUrl is null.但是,在 POST Login 方法中 ReturnUrl 为 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] .我还明确尝试了ReturnUrl作为参数,无论是否有[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.对于 .net core,这是解决问题的方法。

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 :首先,您必须像这样在 get 方法中获取返回 url:

[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.在 get 方法中获取 returnUrl 作为参数,并通过 tempdata 发送到 post 方法。

the post method also like this : post方法也像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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