Why ReturnUrl not showing or null?

winanjaya 146 Reputation points
2023-10-24T07:55:15.09+00:00

Hi

ASP.NetCore 6, why my ReturnUrl always null or not showing in the address bar?

User's image

<form class="needs-validation mt-2" novalidate asp-controller="Auth" asp-action="Login" method="post" autocomplete="off" role="form" >
 <div asp-validation-summary="ModelOnly" class="text-danger"></div>

   <input class="form-control" type="text" asp-for="ReturnUrl" asp-validation-for="ReturnUrl"  value="@( Model.ReturnUrl == null ? "" : Model.ReturnUrl )" hidden />
                         
    <div class="form-group" style="margin-top:25px">
       <label for="input-user_code">Email</label>
       <input class="form-control" type="email" style="text-transform: lowercase" asp-for="userEmail" aria-describedby="login-email" autofocus="true" tabindex="1" autocomplete="off" value="" />
       <span asp-validation-for="userEmail" class="text-danger"></span>
   </div>
   <div class="form-group">
      <label for="input-password">Password</label>
        <input class="form-control form-control-merge" type="password" aria-describedby="login-password" asp-for="userPassword" tabindex="2" autocomplete="new-password" value="" />
        <span asp-validation-for="userPassword" class="text-danger"></span>
   </div>
   <button type="submit" class="btn btn-success btn-lg btn-block btn-ladda mb-3">Log In</button>
</form>

AuthController.cs

 [HttpGet]
 public IActionResult Login(string? ReturnUrl)
 {
   Views.Auth.Data login = new Views.Auth.Data();
   login.ReturnUrl = (ReturnUrl == null ? "" : ReturnUrl);

   return View(login);

program.cs

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Auth/Login";
options.ReturnUrlParameter = "ReturnUrl";
options.AccessDeniedPath = "/Auth/Login";
options.LogoutPath = "/Auth/Logout";
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.Cookie.MaxAge = options.ExpireTimeSpan;
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Qing Guo - MSFT 896 Reputation points Microsoft External Staff
    2023-10-25T03:35:37.4666667+00:00

    Hi @winanjaya,

    You can use [Authorize] on the action which you want to authenticate users. Then it will go to the /Auth/Login and with the ReturnUrl.

    My test result:

    1

    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,

    Qing Guo

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.