开发者

subcontroller/partialrequest and form action method

开发者 https://www.devze.com 2022-12-12 12:52 出处:网络
What I need to do is the following Set small login form (or any partial view) Complete log开发者_Python百科in

What I need to do is the following

  1. Set small login form (or any partial view)
  2. Complete log开发者_Python百科in 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?


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);
        }
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消