Tutorial how to create member login and logout form in Umbraco MVC

Posted written by Paul Seal on June 22, 2016 Umbraco

This post gives you step by step instructions for creating a simple login form with a logout button in Umbraco MVC.

  1. Create the Login Model

    using System.ComponentModel.DataAnnotations;
    
    namespace CodeShare.Models
    {
        public class LoginModel
        {
            [Display(Name = "Username")]
            [Required]
            public string Username { get; set; }
            [Display(Name = "Password")]
            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }
        }
    }


  2. Create a partial view called _Login

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<CodeShare.Models.LoginModel>
    @using Umbraco.Web

    @if(!Umbraco.MemberIsLoggedOn())
    {
        using (Html.BeginUmbracoForm("SubmitLogin", "Member", System.Web.Mvc.FormMethod.Post, new { id="login" }))
        {           
            @Html.AntiForgeryToken()
            @Html.LabelFor(m => m.Username)
            @Html.TextBoxFor(m => m.Username, new { placeholder = "Username" })
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password, new { placeholder = "Password" })
            @Html.ValidationSummary()
            <button name="login" type="submit">Login</button>
        }
    }
    else
    {
        Html.RenderAction("RenderLogout", "Member");
    }


  3. Create a partial view called _Logout

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web

    @if (Umbraco.MemberIsLoggedOn())
    {
        using (Html.BeginUmbracoForm("SubmitLogout", "Member", System.Web.Mvc.FormMethod.Post, new { id = "logout" }))
        {
            @Html.AntiForgeryToken()
            var myUser = System.Web.Security.Membership.GetUser();
            if (myUser != null)
            {
                <p><strong>Logged in as</strong> <span>@myUser.UserName</span></p>
                <button name="login" type="submit"><span>logout</span> <i class="fa fa-arrow-right"></i></button>
            }
        }   
    }


  4. Create a member controller

    using System.Web.Mvc;
    using System.Web.Security;
    using Umbraco.Web.Mvc;
    using CodeShare.Models;

    namespace CodeShare.Controllers
    {
        public class MemberController : SurfaceController
        {
            public ActionResult RenderLogin()
            {
                return PartialView("_Login", new LoginModel());
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult SubmitLogin(LoginModel model, string returnUrl)
            {
                if (ModelState.IsValid)
                {
                    if (Membership.ValidateUser(model.Username, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.Username, false);
                        UrlHelper myHelper = new UrlHelper(HttpContext.Request.RequestContext);
                        if (myHelper.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return Redirect("/login/");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The username or password provided is incorrect.");
                    }
                }
                return CurrentUmbracoPage();
            }
            public ActionResult RenderLogout()
            {
                return PartialView("_Logout", null);
            }
            public ActionResult SubmitLogout()
            {
                TempData.Clear();
                Session.Clear();
                FormsAuthentication.SignOut();
                return RedirectToCurrentUmbracoPage();
            }
        }
    }


  5. Create a login template

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    @{ Html.RenderAction("RenderLogin", "Member"); }


  6. Create a login document type and assign the login template to it. You don't need to add any properties to the document type

  7. Allow the home page to have the login document type as a child node. In latest umbraco (7.4.3) go to the home document type, click on permissions, add child Login, then save.

  8. Create a member in the members section if you don't have one already.

  9. Create the login page in the content content tree underneath the home node.

  10. Build and run the site, go to the login page and you should see something like this. You can now login with the member details for the member you created previously.


    Login page when not logged in yet:



    Login page when logged in:

Create the Login Model

using System.ComponentModel.DataAnnotations;

namespace CodeShare.Models
{
    public class LoginModel
    {
        [Display(Name = "Username")]
        [Required]
        public string Username { get; set; }

        [Display(Name = "Password")]
        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}



Create a partial view called _Login

@inherits Umbraco.Web.Mvc.UmbracoViewPage<CodeShare.Models.LoginModel>
@using Umbraco.Web

@if(!Umbraco.MemberIsLoggedOn())
{
    using (Html.BeginUmbracoForm("SubmitLogin", "Member", System.Web.Mvc.FormMethod.Post, new { id="login" }))
    {           
        @Html.AntiForgeryToken()
        @Html.LabelFor(m => m.Username)
        @Html.TextBoxFor(m => m.Username, new { placeholder = "Username" })

        @Html.LabelFor(m => m.Password)
        @Html.PasswordFor(m => m.Password, new { placeholder = "Password" })

        @Html.ValidationSummary()
        <button name="login" type="submit">Login</button>
    }
}
else
{
    Html.RenderAction("RenderLogout", "Member");
}



Create a partial view called _Logout

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Web

@if (Umbraco.MemberIsLoggedOn())
{
    using (Html.BeginUmbracoForm("SubmitLogout", "Member", System.Web.Mvc.FormMethod.Post, new { id = "logout" }))
    {
        @Html.AntiForgeryToken()
        var myUser = System.Web.Security.Membership.GetUser();
        if (myUser != null)
        {
            <p><strong>Logged in as</strong> <span>@myUser.UserName</span></p>
            <button name="login" type="submit"><span>logout</span> <i class="fa fa-arrow-right"></i></button>
        }
    }   
}



Create a member controller

using System.Web.Mvc;
using System.Web.Security;
using Umbraco.Web.Mvc;
using CodeShare.Models;

namespace CodeShare.Controllers
{
    public class MemberController : SurfaceController
    {
        public ActionResult RenderLogin()
        {
            return PartialView("_Login", new LoginModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitLogin(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.Username, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Username, false);
                    UrlHelper myHelper = new UrlHelper(HttpContext.Request.RequestContext);
                    if (myHelper.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return Redirect("/login/");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The username or password provided is incorrect.");
                }
            }
            return CurrentUmbracoPage();
        }

        public ActionResult RenderLogout()
        {
            return PartialView("_Logout", null);
        }

        public ActionResult SubmitLogout()
        {
            TempData.Clear();
            Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToCurrentUmbracoPage();
        }
    }
}



Create a login template

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}

@{ Html.RenderAction("RenderLogin", "Member"); }



Create a login document type and assign the login template to it. You don't need to add any properties to the document type

Allow the home page to have the login document type as a child node. In latest umbraco (7.4.3) go to the home document type, click on permissions, add child Login, then save.

Create a member in the members section if you don't have one already.

Create the login page in the content content tree underneath the home node.

Build and run the site, go to the login page and you should see something like this. You can now login with the member details for the member you created previously.

1
2

It's as simple as that. Let me know how you get on with this and if there is anything I need to change.

Want to learn more about Umbraco?

Follow along with this video to get you started with Umbraco