Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Steve Morgan 1313 posts 4367 karma points c-trib
    Jun 02, 2014 @ 14:29
    Steve Morgan

    Member Snippets - suggestions for improvements


    I've been helping someone on the forum ;

    We've been using the basic snippets and can achive the following:

    * Secure pages - redirecting the user to the login page - the downside is the user is then redirected to the login page after successfully logging in.

    * Register users - you can't allocate them to a group though which means manual intervention to allow them access to your protected area (if done by group)

    * Show the users login status - works nicely :) 

    So using the snippets you can *nearly* create a fully fledged Members only area part of a site. Suggest the following:

    * login razor snippet is changed to redirect the use to the page they requested that was protected (see below)

    * Update the public / secure access UI option to allow you to specify a member Type (as well as a group)

    * Add to the CreateRegistrationModel Member API an option to specify a group that the user is added to on register.

    Then you could have a fully working members area without having to touch any code (short of creating some snippets).

    Is this possible - happy to have a go at a pull request if someone could point me in the right direction(s). 


    I've created an issue for this

  • Steve Morgan 1313 posts 4367 karma points c-trib
    Jun 02, 2014 @ 14:30
    Steve Morgan

    This is my revised login code from the snippet to avoid the user being redirected to the login page (or perhaps I've missed a setting somewhere!!).

    Would this be a suitable replacement for the current snippet?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers
        // You have to set the login node ID otherwise you'll have an infinite loop if someone hits the login page first
        var loginNodeID = 1072;
        var loginModel = new LoginModel();
        var loginStatusModel = Members.GetCurrentLoginStatus();
    @* NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed *@
        @*  if we don't have a session variable and have a request URL then store it *@
        if (HttpContext.Current.Request.Url != null && HttpContext.Current.Session["redirectURL"] == null) {
            if (CurrentPage.Id != loginNodeID) {
                HttpContext.Current.Session["redirectURL"] = HttpContext.Current.Request.Url.ToString();
    @if (loginStatusModel.IsLoggedIn)
        var redirectUrl = (string)HttpContext.Current.Session["redirectURL"];
        var currentUrl = HttpContext.Current.Request.Url.ToString();
        if (HttpContext.Current.Session["redirectURL"] != null) {
            // clear the session variable for future logins
            HttpContext.Current.Session["redirectURL"] = null;
        else {
            // Nothing in the session so we will go home 
    @using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
            @Html.ValidationSummary("loginModel", true)
            @Html.LabelFor(m => loginModel.Username)
            @Html.TextBoxFor(m => loginModel.Username)
            @Html.ValidationMessageFor(m => loginModel.Username)
            <br />
            @Html.LabelFor(m => loginModel.Password)
            @Html.PasswordFor(m => loginModel.Password)
            @Html.ValidationMessageFor(m => loginModel.Password)
            <br />
  • Steve Morgan 1313 posts 4367 karma points c-trib
    Jun 02, 2014 @ 14:35
    Steve Morgan
    Oops double post
  • John A 20 posts 44 karma points
    Jul 05, 2014 @ 20:23
    John A

    "* Add to the CreateRegistrationModel Member API an option to specify a group that the user is added to on register."

    • I encountered the exact same limitation. It would be great if a Membership Group could be specified when using Members.CreateRegistrationModel()
  • bob baty-barr 1180 posts 1294 karma points MVP
    Aug 22, 2014 @ 14:53
    bob baty-barr

    with the help of the amazing Casey Neehouse, i was able to add a class to app_code to assign a new member to a role when created.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    namespace MyApplication.Events
    public class MemberEvents : ApplicationEventHandler
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    Umbraco.Core.Services.MemberService.Created += MemberService_Created;

    void MemberService_Created(Umbraco.Core.Services.IMemberService sender, Umbraco.Core.Events.NewEventArgs<IMember> e)
    sender.AssignRole(e.Entity.Id, "Site Visitors");


Please Sign in or register to post replies

Write your reply to: