Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Login ReturnUrl always NULL?

People also ask

Why returnUrl is null in MVC?

This is actually by design. The returnUrl parameter is only populated automatically when you try to access an authorized resource but were not authenticated or authorized.

How to use returnUrl in ASP net MVC?

Solution 1 1) In your UserLogin class add a property called returnUrl. Then, in the html form puet a input of type hidden whit this value (in order to be included in the UserLogin). Obviously in the controller you will recibe only one parameter of type UserLogin and you can access the returnUrl from there.


This tends to happen when you're using one generic logon form, but you're explicitly specifying the Controller and ActionMethod (which is causing a form post, but losing the querystring)

Just to clarify, this is what your code should look like in your BeginForm:

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

EDIT: This is by design as RickAnd mentions in comments below. However it doesn't allow for the UI pattern of being deep in a site, clicking on LogOn, then returning to the page you were previously on, if it allows anonymous users. It's a commonly requested pattern. David Allen's approach to LogOff would also work nicely for a clean redirect at LogOn.


Maybe you don't include the ReturnURL parameter into you login form's action attribute, thus posting to a URL without that parameter?


Basically, The Asp.net MVC has some hidden features. For Example when you pass variable 'id' to controller action, it interprets 'id' as default identifier and puts it on browser query with fore slash.By using another name instead of 'id' we will see '?' rather than fore slash. Because of setting the 'id' name on RegisterRoutes method on global.asax file.

In this Problem you have created a custom data passer to controller by using this code:

using(Html.BeginForm("LogOn", "Account", FormMethod.Post))
{
//form fields
}

So Asp.net MVC ignores other useful data to pass to controller action, and we'll see returnUrl always null.

While, by using this, Asp.net MVC acts Correctly and returnUrl is mounted:

using(Html.BeginForm())
{
//form fields in LogOn View
}

By the way, When we use custom data passer to controller action, must pass another data manually like this:

using(Html.BeginForm("LogOn", "Account", new {ReturnUrl = Request.QueryString["ReturnUrl"] }))
{
//form fields
}

There are two ways I can think of to deal with logon and logoff scenarios. Dave Beer outlined one way, above. There is another approach that works in many situations. I used it when I coded the NerdDinner tutorial. The tutorial provides us with a logoff function that logs you off and takes you home. I did not want that. I wanted to return to the page I was on before I logged off. So I modified my Account controller logoff action to look like this

   public ActionResult LogOff()
    {
        FormsService.SignOut();
        return Redirect(Request.UrlReferrer.ToString());
    }

You can get fancier and pass in a returnUrl and test for it, in case you want to override this behavior. But I don't need that. This achieves the desired result. The Logon can work similarly. Maybe there are ways to use the MVC framework to do this for me, but until I learn them, this is VERY simple and works reliably.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!