What I need to do is the following
- Set small login form (or any partial view)
- Complete log开发者_Python百科in actions
- 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?
You could try the following:
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" /> <% } %>
Include this partial in some view page:
<% if (!User.Identity.IsAuthenticated) { %> <% Html.RenderPartial("~/Views/Home/Login.ascx"); %> <% } else { %> Welcome <%= Html.Encode(User.Identity.Name) %> <%} %>
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); } }
精彩评论