Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A bunch of Umbraco Membership API snippets
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security.
using Umbraco.Core.Services;
using Umbraco.Web.Models;
using Umbraco.Web.PublishedCache;
* MembershipHelper
// Creates a new register model. Will be constructed with member properties defined on the "MyMemberTypeAlias" within Umbraco.
var newMember = this.Members.CreateRegistrationModel("MyMemberTypeAlias");
* Other ways of creating a RegisterModel. Avoid!
// Nay! Assumes that a default member type for a baked in alias exists!
newMember = this.Members.CreateRegistrationModel();
// Yay for mutability! >_>
/* Nay! Attempts to manually construct an instance of MembershipHelper based off the
* current context and also assumes that a default member type for a baked in alias exists! */
newMember = new RegisterModel();
/* Nay! Creates a completely vanilla instance of the model and also assumes that a default
* member type for a baked in alias exists! */
newMember = RegisterModel.CreateModel();
* Standard properties that are required to be set for the registration to succeed.
newMember.Name = "Mike Bowen";
newMember.Email = "";
newMember.Password = "24DaysInUmbraco";
// UsernameIsEmail needs to be set to false in order for the Username property to be persisted.
newMember.UsernameIsEmail = false;
newMember.Username = "mike";
newMember.LoginOnSuccess = false;
* Setting custom properties defined on the member type. The property must be set as "Member can edit" within the Umbraco interface.
UmbracoProperty favouriteLanguage = newMember.MemberProperties.Single(p => p.Alias == "favouriteLanguage");
favouriteLanguage.Value = "Crystal";
* Saving the member.
MembershipCreateStatus status = new MembershipCreateStatus();
MembershipUser member = this.Members.RegisterMember(newMember, out status);
// Check the status of the registration.
if (status == MembershipCreateStatus.Success)
// Add a role ("member group" in Umbraco) to the newly created member.
Roles.AddUserToRole(newMember.Username, "MyMemberGroup");
// Members are automatically approved.
member.IsApproved = false;
// Re-save the member.
* MemberService
IMemberService service = ApplicationContext.Current.Services.MemberService;
// Creates a new member object to persist later on.
IMember newMember = service.CreateMember("", "", "Mike Bowen", "MyMemberType");
// Ostensibly the same methods. Creates and immediately persists a new member.
newMember = service.CreateMemberWithIdentity("", "", "Mike Bowen", "MyMemberType");
newMember = service.CreateWithIdentity("", "", "Mike Bowen", "MyMemberType");
// Setting custom properties.
newMember.SetValue("favouriteLanguage", "Ruby");
// Setting standard membership properties. Requires less hoop jumping than using MembershipHelper.
newMember.Comments = "Here's an member";
newMember.IsApproved = false;
newMember.PasswordQuestion = "Awesome?";
newMember.RawPasswordAnswerValue = "AWESOME";
service.SavePassword(newMember, "24DaysInUmbraco");
// Assigning roles.
service.AssignRoles(new [] { newMember.Id }, new [] { "MyMemberGroup", "MyOtherMemberGroup" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment