How to Create Members in Umbraco
Taking a closer look at Umbraco's MembershipHelper

Last updated on 14 Dec 2016 | Posted on 10 Dec 2016

To get started with Umbraco's MembershipHelper we need to instantiate it by passing in the 'UmbracoContext' available when your code is within context of Umbraco such as inside a surface controller.

So, here's the instantiation:

MembershipHelper oMember = new MembershipHelper(UmbracoContext);

In order to create a member we must create a RegisterModel object via the members CreateRegistrationModel, like this:

RegisterModel oReg = oMember.CreateRegistrationModel();

We can then assign all of the necessary properties of the new RegisterModel object's values accordingly:

oReg.Name = "memberName";
oReg.UsernameIsEmail = true;
oReg.Email = "a@b.com";
oReg.LoginOnSuccess = true;
oReg.MemberTypeAlias = "typeAlias";
oReg.Password = "vSdVsdv";

This next part is optional. As far as I know, this is used to add in any custom properties that can be added via Umbraco's Members tab. It's quite common to want or need to extend the base member architecture by creating custom fields. In code, this is done by creating a list of UmbracoProperty objects like this:

List<UmbracoProperty> props = new List<UmbracoProperty>();
UmbracoProperty p = new UmbracoProperty();
p.Name = "name";
p.Alias = "alias";
p.Value = "value";

Note. The capitalisation of the alias is critical.

We now assign that collection to the MemberProperties property of the RegisterModel object:

props.Add(p);
oReg.MemberProperties = props;

Finally, we need to call the RegisterMember method of the MembershipHelper object which requires a RegisterModel and effectively a Microsoft MembershipCreateStatus object which can be seen below:

MembershipCreateStatus oStatus = new MembershipCreateStatus();
if (oStatus == MembershipCreateStatus.Success)
{
	MembershipUser oMembershipUser = oMember.RegisterMember(oReg, out oStatus);

	// Do whatever else you need to do with the member
}

The complete code if you want it is:

MembershipHelper oMember = new MembershipHelper(UmbracoContext);
RegisterModel oReg = oMember.CreateRegistrationModel();
oReg.Name = "memberName";
oReg.UsernameIsEmail = true;
oReg.Email = "a@b.com";
oReg.LoginOnSuccess = true;
oReg.MemberTypeAlias = "typeAlias";
oReg.Password = "vSdVsdv";

List<UmbracoProperty> props = new List<UmbracoProperty>();
UmbracoProperty p = new UmbracoProperty();
p.Name = "name";
p.Alias = "alias";
p.Value = "value";

props.Add(p);
oReg.MemberProperties = props; 

MembershipCreateStatus oStatus = new MembershipCreateStatus();
if (oStatus == MembershipCreateStatus.Success)
{
	MembershipUser oMembershipUser = oMember.RegisterMember(oReg, out oStatus);

	// Do whatever else you need to do with the member
}
How to Create a Custom Umbraco backend section

How to Create a Custom Umbraco backend section

Posted on 19 Nov 2016

This article focuses on creating your own custom backend Umbraco 7+ section. Umbraco installs with the following built in sections:

  • Content
  • Media

Read more

How to Create Custom Examine Settings in Umbraco

How to Create Custom Examine Settings in Umbraco

Posted on 28 Jan 2016

Umbraco has both an internal and external indexer. The idea behind this is to provide an index for all back office searching within the Umbraco CMS. T…

Read more