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:

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

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.