I working on asp.net core mvc project with .net 7. I want create return URL for my sign in page. so for example when the user in the shop page and click on sign in link. after sign in user back to that page. so my action for show the item detail in shop controller is like this:
public IActionResult detailItemShow(string name, string successMessage = "")
{}
I create return URL and sign in page link in my rezor View with this code:
<a href="@Url.Action("Index", "Sign", new { returnUrl =(HttpContextAccessor.HttpContext.Request.QueryString) })" class="btn btn-warning text-dark my-2">login</a>
and this is my login action code:
public IActionResult Index(string? returnUrl = null)
{
if(signInManager.IsSignedIn(User))
{
return RedirectToAction("Index","Home");
}
ViewData["returnUrl"] = returnUrl;
return View("SignIn");
}
and I use this code after the login successful for redirect user:
if (SignInResult.Succeeded)
{
if(!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Account", new {username=user.UserName });
}
but the if section always is false and the return URL is not working. after I debug my code I find for example my URL for page is something like this: (my name parameter here is "test product 13")
https://localhost:7089/Shop/detailItemShow?name=test%20product%2013
but the return URL is create like this:
returnUrl=%3Fname%3Dtest%2520product%252013
so the "Url.IsLocalUrl(returnUrl)" always is false the the if body not run. also when I remove this I get the page is not working.
how to fix this problem? please help?