Click here to Skip to main content
14,870,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi when I am trying forms authentication in mvc returnurl is coming null when user trying to call one page.

below is my same code

In Home controller login I am writing the following code

public ActionResult Index()
            return View();

        public ActionResult Login(string ReturnUrl)
            if (Request.Form["UserName"] == "abcsde" && Request.Form["Password"] == "kusdfasdfmar")
                FormsAuthentication.SetAuthCookie("abcsde", true);
                if (!string.IsNullOrEmpty(ReturnUrl))

            return View("Index");
        public ActionResult Default()
            return View();

in Indexview page following code is return

    ViewBag.Title = "Login";
    ViewBag.ReturnUrl = Request["ReturnUrl"];

@using (Html.BeginForm("Login", "Login", FormMethod.Post, new { ReturnUrl = Request.QueryString["ReturnUrl"]}))
   <input type =submit value="Login" />


so when user trying for default page it is successfully redirecting to the login page but because of return url null the page is not redirecting to the default page .kindly help in this regard
Updated 26-Sep-15 9:11am

I really believe that you need to be using the Request object, instead of getting the QueryString property, because in the code above this one, you're using the same method I'm talking about. The value is null, only because there is

  1. either no QueryString with this name
  2. or the value of it is null
  3. or the data was sent as a POST request, not as GET; same as option 1.

Html.BeginForm("Login", "Login", FormMethod.Post, 
                new { ReturnUrl = Request["ReturnUrl"]});

.. this would get the ReturnUrl from the Request, whether it is attached as a QueryString, or passed through a Form. QueryString method would only return a string as a value, if there is a QueryString for ReturnUrl appended to the URL (such as this one page?ReturnUrl=defaultPage). Whereas, the Request method will capture the data even if it was passed from the form, or if it is attached to the URL; in both cases.

I would myself, go with the simple Request method of getting the data, because it a chance of data being passed from a form with a POST method, in this case there is no QueryString and not only this, but many other conditions also show up where .QueryString method fails.
In order to have ReturnUrl populated (and you login page function as it should), you need to tell ASP.NET framework which action is your login action - as it is not straightforward.
So, you need to adjust your web.config
<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="30" name=".MVC3FORMSAUTH" cookieless="UseCookies" slidingExpiration="true" protection="All"/>

See:[^]. As you see there are other properties to consider, so check the rest of the properties also:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900