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

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

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.

Related Posts

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post

How to send Slack messages programmatically using C#

In this post I show you how you can send Slack messages programmatically using C#

Read Post

How to fix the SVG 404 error when using Umbraco and Azure Blob Storage

In this post I show you how to solve the SVG 404 error when working with Umbraco and Azure Blob Stor…

Read Post