Login Page in umbraco

Step I: The first step is to create a member group and some members that belong to the group.
MemberGroup
Here I have created a group External Users

Step II: Create some sample users and add them to External users group.

Step III: Now that we have some users to work with our next step is to code. Create an mvc project in visual studio and then a model as show belos


public class MemberLoginModel
{
    [Required, Display(Name = "Enter your user name")]
    public string UserName { get; set; }
    [Required, Display(Name = "Enter your password")]
    public string Password { get; set; }
    public bool RememberMe { get; set; }
}

Step IV: Now we create surface controller as shown below.


public class MemberLoginSurfaceController : SurfaceController
{
    [HttpGet]
    [ActionName("MemberLogin")]
    public ActionResult MemberLoginGet()
    {
        return PartialView("MemberLogin", new MemberLoginModel() { UserName = "Enter Username", Password = "Enter Password" });
    }

    [HttpGet]
    public ActionResult MemberLogout()
    {
        Session.Clear();
        FormsAuthentication.SignOut();
        return Redirect("/");
    }

    [HttpPost]
    [ActionName("MemberLogin")]
    public ActionResult MemberLoginGet(MemberLoginModel model)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                Session["username"] = model.UserName;
                return Redirect("/student");
            }
            else
            {
                return RedirectToCurrentUmbracoPage();
            }
        }
        return RedirectToCurrentUmbracoPage();
    }
}

Step V: Build the project and copy the dll to bin folder in umbraco. Create a partial view as shown below


@model MvcMemberLogin.Models.MemberLoginModel
@if (Session["username"]!=null)
{
    <p>Logged in : @Session["username"]</p>
    <p>@Html.ActionLink("Log out", "MemberLogout", "MemberLoginSurface")</p>
}
else
{
    using (Html.BeginUmbracoForm("MemberLogin", "MemberLoginSurface"))
    {
        @Html.EditorFor(x => Model)
    }
}

Step VI: Use the partial view in any template as follows


@Html.Action("MemberLogin","MemberLoginSurface")
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s