Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC redirect after logon to the page a user came from

Let's take as an example a standard default ASP.NET MVC project created by Visual Studio.

Being on ../Home/About page I click Log On link and get to the ../Account/LogOn page. After logging in I'm being redirected to the Home page, not to the Home/About page from which I get to the LogOn page. Why is that?

Though, in the AccountController we have:

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Is the problem in the fact that string returnUrl is empty after clicking Log On link? How to assign it a value when clicking the Log on link?

How to redirect a user, after logging in, to the page, from which he/she came from?

View code:

<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Log On</h2>
<p>
    Please enter your username and password. <%: Html.ActionLink("Register", "Register") %> if you don't have an account.
</p>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                <%: Html.LabelFor(m => m.UserName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(m => m.UserName) %>
                <%: Html.ValidationMessageFor(m => m.UserName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(m => m.Password) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(m => m.Password) %>
                <%: Html.ValidationMessageFor(m => m.Password) %>
            </div>

            <div class="editor-label">
                <%: Html.CheckBoxFor(m => m.RememberMe) %>
                <%: Html.LabelFor(m => m.RememberMe) %>
            </div>

            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
<% } %>

Edited (added):

It appeared that above view code is not very relevant to the problem, instead I should have looked at this (LogOnUserControl.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
    Welcome <b><%: Page.User.Identity.Name %></b>!
    [ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
}
else {
%> 
    [ <%: Html.ActionLink("Log On", "LogOn", "Account")%> ]
<%
}
%>
like image 241
rem Avatar asked Oct 22 '10 08:10

rem


People also ask

What is the difference between redirect and RedirectToAction in MVC?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


1 Answers

When you generate the LogOn link you need to pass the returnUrl query string parameter:

<%: Html.ActionLink("Log On", "LogOn", "Account", new { returnUrl = Request.Url }, null)%>
like image 82
Darin Dimitrov Avatar answered Nov 07 '22 13:11

Darin Dimitrov