public HttpResponseMessage DeleteSubAccount(RegisterSubModel model)
 {
     if (ModelState.IsValid)
     {
         // Attempt to register the user
         try
         {
             if (WebSecurity.UserExists(model.EmailAddress))
             {
                 ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(model.EmailAddress);
                 ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.EmailAddress, true);
                 return Request.CreateResponse(HttpStatusCode.OK, "{}");
             } else
             {
                 ModelState.AddModelError("emailaddress", "Sorry, the user no longer exists.");
                 return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
             }
         }
         catch (Exception ex)
         {
             Logger.Log(ex);
             ModelState.AddModelError("", "An unexpected error occured");
             return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
     }
     else
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
 }
 public HttpResponseMessage CreateSubAccount(RegisterSubModel model)
 {
     if (ModelState.IsValid)
     {
         // Attempt to register the user
         try
         {
             if (!WebSecurity.UserExists(model.EmailAddress))
             {
                 WebSecurity.CreateUserAndAccount(model.EmailAddress, "secretPassword", new { DisplayName= model.Username, ParentUserName = User.Identity.Name, IsSubAccount = true });
                 string token = WebSecurity.GeneratePasswordResetToken(model.EmailAddress, 7 * 24 * 60);
                 // send an email with the token to the created user
                 SecurityHelper.SendCreatedSubaccountEmail(token, model.EmailAddress, model.Username);
                 return Request.CreateResponse(HttpStatusCode.OK, "{}");
             }
             else {
                 ModelState.AddModelError("email", "A user with this emailadress already exists.");
                 return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
             }
         }
         catch (MembershipCreateUserException e)
         {
             Logger.Log(e);
             ModelState.AddModelError(e.Message, e.StatusCode.ToString());
             return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
         catch (Exception ex)
         {
             try
             {
                 ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(model.EmailAddress);
                 ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.EmailAddress, true);
             }
             catch (Exception)
             {
                 // ignore
             }
             Logger.Log(ex);
             ModelState.AddModelError("", "An unexpected error occured");
             return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
     }
     else
     {
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
 }