If you are using umbraco 7 it is best to user the member service. Below is a simple approach you could employ to achieve this.
Please note ApplicationContext no longer exists for new umbraco versions think from 8+ So just ommit that. Everything else works fine.
public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
int umbracoMemberId = -1;
if (!MemberExists(emailAddress))
{
IMember newMember = ApplicationContext.Current.Services.MemberService.CreateMember(emailAddress, emailAddress, memberName, memberTypeAlias);
try
{
ApplicationContext.Current.Services.MemberService.Save(newMember);
ApplicationContext.Current.Services.MemberService.SavePassword(newMember, memberPassword);
ApplicationContext.Current.Services.MemberService.AssignRole(newMember.Id, memberGroupName);
umbracoMemberId = newMember.Id;
}
catch (Exception ex)
{
throw new Exception("Unable to create new member " + ex.Message);
}
}
return umbracoMemberId;
}
public bool MemberExists(string emailAddress)
{
return (ApplicationContext.Current.Services.MemberService.GetByEmail(emailAddress) != null);
}
For a more authentic v8 approach you will want to discard ApplicationContext completely and instead use the dependency injection engine as below. This example assumes you are not able to inject the dependency in this case IMemeberService through the constructor so should cover most cases for most people at least from what I have come across so far especially for those developing applications with umbraco
public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
int umbracoMemberId = -1;
if (!MemberExists(emailAddress))
{
try
{
IMemberService _memberService = Current.Factory.GetInstance<IMemberService>();
IMember newMember = _memberService.CreateMember(body.Email, body.Email, celebName, "Member");
_memberService.Save(newMember);
_memberService.SavePassword(newMember, body.Password);
_memberService.AssignRole(newMember.Id, CMSContentConstants.RootNodes.gGourpAliasCelebrityMember);
umbracoMemberId = newMember.Id;
}
catch (Exception ex)
{
throw new Exception("Unable to create new member " + ex.Message);
}
}
return umbracoMemberId;
}