Click here to Skip to main content
15,887,683 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

SQL
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))
                    Response.Redirect(ReturnUrl);
                else

                    Response.Redirect(FormsAuthentication.LoginUrl);
            }
            return View("Index");
        }
        [Authorize]
        public ActionResult Default()
        {
            return View();
        }



in Indexview page following code is return

@{
    ViewBag.Title = "Login";
    ViewBag.ReturnUrl = Request["ReturnUrl"];
}

<h2>Login</h2>
@using (Html.BeginForm("Login", "Login", FormMethod.Post, new { ReturnUrl = Request.QueryString["ReturnUrl"]}))
{
   @Html.TextBox("UserName")
   <br></br>
   @Html.TextBox("Password")
   <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
Posted
Updated 26-Sep-15 9:11am
v2

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.


C#
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.
 
Share this answer
 
v2
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
XML
<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="30" name=".MVC3FORMSAUTH" cookieless="UseCookies" slidingExpiration="true" protection="All"/>
    </authentication>

See: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.loginurl(v=vs.110).aspx[^]. As you see there are other properties to consider, so check the rest of the properties also: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication(v=vs.110).aspx[^]
 
Share this answer
 
v2

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