Hi @Dondon510 ,
how to change returnUrl path (ASP MVC Net6)?, currently it goes to Account/Login?ReturnUrl=blahblah
although I never set it to Account/Login (may be it's default?), I want to change it to User/Login
From your description, it seems that you want to set the login path, instead of the ReturnUrl
.
In this scenario, you can check the Program.cs
file and the _LoginPartial.cshtml
file.
Generally, when configure the authentication and authorization, we could set the login page via the LoginPath
property in the Program.cs file, like this (Code from the Asp.net core Identity configuration, refer this article):
builder.Services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login"; //set the login path.
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
Besides, we could also change the login button's URL, open the Views/Shared/_LoginPartial.cshtml page, then you can change the Login button's path:
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity?.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>
Note: In your project, since you are using Account/Login
, the controller name should be Account
, in this scenario, to after change the Login path, you should also change the controller name to User
or use the Route
attribute to change the request path. Code like this:
Account controller:
[Route("/User/Login")]
public IActionResult Login()
{
return View();
}
The Login Button:
<a class="nav-link text-dark" href="/User/Login">User Login</a>
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion