I have an MVC4 application with Membership logon (through FormsAuthentication).
This is defined in web.config as follows. My default url is home root (~/):
<roleManager enabled="true" />
<authentication mode="Forms">
<forms defaultUrl="~" loginUrl="~/Account" />
</authentication>
In my AccountController in the Login post method, following code is relevant. This code is executed when the user clicks on the login with valid credentials.
if (Membership.ValidateUser(creds.Username, creds.Password))
{
FormsAuthentication.RedirectFromLoginPage(creds.Username, false);
return null;
}
Now, if I'm navigating (anonymously) to: ~/Admin, I get redirected to ~/Account to log in, which is perfect. I can see that the url is formed as follows:
http://localhost:23759/Account?ReturnUrl=%2fAdmin
But, when I succesfully login, I get redirected to home (~/) instead of ~/Admin
Please help! Many thanks!
Edit: Found the actual issue: it was the post method that wasn't receiving the querystring
C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".
" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.
I found the solution! Thanks to FlopScientist, who got me thinking further.
It was indeed because I was doing a POST method, which did not take the QueryString from the GET into account.
First I had this in my View:
@using (Html.BeginForm("Index", "Account")
{
<div class="LoginBox">
//Etc...
</div>
}
I have updated it to following:
@using (Html.BeginForm("Index", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{
//Etc...
}
Now I can actually see a querystring in my debug and I do get a correct redirect!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With