Walters
Walters

Reputation: 131

Why is returnUrl value always null?

When I try login in, returnUrl is always null.

I tried answers from existing questions however nothing seems to work for my case.

My Startup:

services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme ).AddCookie( options => {
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
    options.LoginPath = "/home";
    options.AccessDeniedPath = "/Error/401";
    options.LogoutPath = new PathString("/home/logout");
});

Controller:

[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
{
   try{
    if (valid){
      if (!String.IsNullOrEmpty(returnUrl))
          return LocalRedirect(returnUrl);
      }
  }

Url: http://localhost:8888/home?ReturnUrl=%2FRequest%2FNotes%2F1

Login View:

<form method="post" asp-action="Login">

@if (TempData["ErrorMessage"] != null)
                {
                    <div class="alert alert-warning alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aira-label="close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        @TempData["ErrorMessage"]
                    </div>
                }
                else if (ViewBag.SessionTimeout != null)
                {
                    <p class="error">@ViewBag.SessionTimeout</p>
                }
                <div class="form-group">
                    <input class="form-control" asp-for="Username" placeholder="Username" data-val="true" data-val-required="Your username is required." />
                    <span data-valmsg-for="Username" data-valmsg-replace="true" class="field-validation-error"></span>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" asp-for="Password" placeholder="Password" data-val="true" data-val-required="Your password is required." />
                    <span data-valmsg-for="Password" data-valmsg-replace="true" class="field-validation-error"></span>
                </div>

                <div class="form-action">
                    <button type="submit" class="btn btn-danger btn-invert btn-wide">Login</button>
                </div>
                @*<div class="form-meta">
                    <p>Forgot your password? <a href="#">Reset it.</a></p>
                </div>*@
            </form>

Upvotes: 0

Views: 168

Answers (1)

Mosia Thabo
Mosia Thabo

Reputation: 4267

You did not submit the ReturnUrl with your form. I am going to assume that you stored your ReturnUrl in a ViewData dictionary within HttpGet and passed to the View.

In your view you will have to pass that ReturnUrl back to the controller as a query string. Just add the following on your form:

<form method="post" asp-action="Login" asp-route-ReturnUrl="@ViewData["ReturnUrl"]">

This will pass the ReturnUrl back to the controller with the Post request as a query string. asp-route Helper allows you to add a single query string to your url/request.

Upvotes: 1

Related Questions