4
public ActionResult LogOn(string returnUrl)
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction(string.Empty, "home");
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
               //http://localhost:666/en-us/account/logon?returnurl=%2fen-us%2fadminka
                     //..............
                }
                return View();

            }
        }

        [HttpPost]
        public ActionResult LogOn(LogOnViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (....)
                {
                    //..............
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        return Redirect(returnUrl);
                    return RedirectToAction(string.Empty, "home");

                }
                else
                {
                    //..............
                }
            }

            return View(model);
        }

In HttpPost LogOn returnUrl is always equals null, even if it was not null in HttpGet LogOn.

Why? How do I fix it?

0

3 Answers 3

11

You need the returnUrl to be posted with the form post.

Probably the cleanest solution is to add returnUrl as a property to the LogOnViewModel:

    public class LogOnViewModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string ReturnUrl { get; set; }
}

Your get method would set that value:

[HttpGet]
public ActionResult LogOn(string returnUrl)
    {
       // code for already authenticated case left out for demo
            var model = new LogOnViewModel { ReturnUrl = returnUrl };
            return View(model);

        }
    }

In your form, you would persist that value as a hidden field:

@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.ReturnUrl)

    // rest of form code left out for demo purposes
}

Your post method would then have access to that value:

    [HttpPost]
    public ActionResult LogOn(LogOnViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (....)
            {   string returnUrl = model.ReturnUrl;

                //..............
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    return Redirect(returnUrl);
                return RedirectToAction(string.Empty, "home");

            }
            else
            {
                //..............
            }
        }

        return View(model);
    }
Sign up to request clarification or add additional context in comments.

Comments

2

In your login view, add the ReturnUrl parameter to the form action. For instance:

BeginForm("LogOn", "Account", new {ReturnUrl = Request.QueryString["ReturnUrl"]})

See davewasthere's answer.

Comments

0

When in the LogOn view verify that :

  • either the url contains "returnUrl"
  • or there is a returnUrl field in the form

3 Comments

<<either the url contains "returnUrl">> I'm sorry, what do you mean? There's ?returnurl= parameter in the first action LogOn(string returnUrl) and in the Url too.
The form doesn't have a returnUrl field.
You should add a returnUrl field in your form then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.