views:

75

answers:

1

What I need to do is the following

  1. Set small login form (or any partial view)
  2. Complete login actions
  3. Get back to the SAME place (controller/action) user was before clicking LOGIN button

Any of Partial Request/Subcontroller,RenderAction solutions offer this without any extra code to handle parent page url?

A: 

You could try the following:

  1. Create a new user MVC User Control (Login.ascx) in which you setup a login form:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <% using (Html.BeginForm("authenticate", "login")) { %>
        <%= Html.Hidden("returnurl", Request.Url.ToString()) %>
        <div>
            <label for="username">Username</label>
            <%= Html.TextBox("username") %>
        </div>
        <div>
            <label for="password">Password</label>
            <%= Html.TextBox("password") %>
        </div>
        <input type="submit" value="Login" />
    <% } %>
    
  2. Include this partial in some view page:

    <% if (!User.Identity.IsAuthenticated) { %>
        <% Html.RenderPartial("~/Views/Home/Login.ascx"); %>
    <% } else { %>
        Welcome <%= Html.Encode(User.Identity.Name) %>
    <%} %>
    
  3. In your login controller verify username and password and if authentication is successful redirect to returnUrl:

    public class LoginController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Authenticate(string username, string password, string returnurl)
        {
            // TODO: Perform authentication, set cookies,
            // verify that returnurl belongs to your site, etc...
            return Redirect(returnurl);
        }
    }
    
Darin Dimitrov
Is this the best solution? WHat's with TempData passing?Does SubControllers solve this ?
Andrej Kaurin