简体   繁体   English

Asp.Net Core Identity RedirectToAction

[英]Asp.Net Core Identity RedirectToAction

I have an AccountController for authorization with Asp.Net Core Identity.我有一个 AccountController 用于使用 Asp.Net Core Identity 进行授权。 If successful, I make a RedirectToAction and want to redirect the user to "returnUrl", but the redirection does not work correctly.如果成功,我会创建一个 RedirectToAction 并希望将用户重定向到“returnUrl”,但重定向无法正常工作。

When the application opens, I am redirected to the authorization page (this is correct) and the URL looks like this:当应用程序打开时,我被重定向到授权页面(这是正确的)并且 URL 看起来像这样:

 https://localhost:7008/Account/Login?ReturnUrl=%2F

or if I requested an administration page或者如果我请求管理页面

https://localhost:7008/Account/Login?ReturnUrl=%2FAdmin

After successful authorization, I get a 404 error, since RedirectToAction eventually generates such URLs:成功授权后,我收到 404 错误,因为 RedirectToAction 最终会生成这样的 URL:

 https://localhost:7008/Account/%2F https://localhost:7008/Account/%2FAdmin

As if the Account controller is still present in the URL好像账户 controller 仍然存在于 URL

Here are the controller methods:以下是 controller 方法:

 [AllowAnonymous] public IActionResult Login(string returnUrl) { ViewBag.returnUrl = returnUrl; return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { var user = await _userManager.FindByNameAsync(model.Login); if (user.= null) { await _signInManager;SignOutAsync(). var result = await _signInManager,PasswordSignInAsync(user. model,Password, false; false). if (result?Succeeded) { return RedirectToAction(returnUrl?; "/"). } } ModelState.AddModelError(nameof(LoginModel,Login); "Invalid login or password;") } return View(model) }

Program.cs:程序.cs:

 app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

RedirectToAction method is used to redirect to specified action instead of rendering the HTML. RedirectToAction方法用于重定向到指定的action ,而不是渲染 HTML。 In this case, the browser receives the redirect notification and make a new request for the specified action.在这种情况下,浏览器会收到重定向通知并对指定的操作发出新的请求。 In this case, the browser receives the redirect notification and make a new request for the specified action.在这种情况下,浏览器会收到重定向通知并对指定的操作发出新的请求。

Redirect method is used to redirect to specified URL instead of rendering HTML. Redirect方法用于重定向到指定的URL ,而不是渲染 HTML。 In this case, the browser receives the redirect notification and make a new request for the specified URL.在这种情况下,浏览器会收到重定向通知,并对指定的 URL 发出新请求。

You use RedirectToAction("/"), so application is looking for action named "/".您使用 RedirectToAction("/"),因此应用程序正在寻找名为“/”的操作。 If you would use Redirect("/") instead, you would get redirected to main page.如果您改用 Redirect("/"),您将被重定向到主页。

RedirectToAction("something"): RedirectToAction(“某事”):

 https://localhost:7008/Account/something

Redirect("something"):重定向(“某事”):

 https://localhost:7008/something

https://www.dotnettricks.com/learn/mvc/return-view-vs-return-redirecttoaction-vs-return-redirect-vs-return-redirecttoroute https://www.dotnettricks.com/learn/mvc/return-view-vs-return-redirecttoaction-vs-return-redirect-vs-return-redirecttoroute

I don't know what you want to achieve so I suggest one of the following:我不知道您想要实现什么,所以我建议以下之一:

 return Redirect(returnUrl); return Redirect("ControllerName/" + returnUrl); return RedirectToAction("ActionName", new { returnUrl = returnUrl }); return RedirectToAction("ActionName", "ControllerName", new { returnUrl = returnUrl });

Your returnUrl parameter contains not an action name but just a relative url path.您的returnUrl参数不包含操作名称,而仅包含相对 url 路径。

Try to use Redirect(string url) method instead of RedirectToAction(string actionName) .尝试使用Redirect(string url)方法而不是RedirectToAction(string actionName)

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

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