Create users

When creating a user, you must perform the following steps:

  1. Get instances of the user and user profile managers.
    The managers are represented by the UserManager and UserProfileManager classes. For more information about getting their instances, see Managing users.
  2. Create the user.
    To create the user call the CreateUser method of the UserManager class and pass the required arguments. The CreateUser method accepts an output parameter of type System.Web.Security.MembershipCreateStatus, that provides information about the result of the operation. The method returns the instance of the created user. For more information, about the User class, see Users.
  3. Check whether the user has been created successfully.
    If the user has been created successfully, the value of the output parameter must be equal to MembershipCreateStatus.Success.
  4. Create a user profile for the user.
    If the user has been create successful, you create a user profile for him. To do this, call the CreateProfile method of the UserProfileManager class and pass the required parameters. For the type of profile you use SitefinityProfile. For more information, see For developers: User profiles.
  5. Save the changes.
    Finally, you save the changes to both UserManager and UserProfileManager.

Here is a code example:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class ManagingUsersSnippets
    {
        public static MembershipCreateStatus CreateUser(string username, string password, string firstName, string lastName, string mail, string secretQuestion, string secretAnswer, bool isApproved)
        {
            UserManager userManager = UserManager.GetManager();
            UserProfileManager profileManager = UserProfileManager.GetManager();

            System.Web.Security.MembershipCreateStatus status;

            User user = userManager.CreateUser(username, password, mail, secretQuestion, secretAnswer, isApproved, null, out status);

            if (status == MembershipCreateStatus.Success)
            {
                SitefinityProfile sfProfile = profileManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile;

                if (sfProfile != null)
                {
                    sfProfile.FirstName = firstName;
                    sfProfile.LastName = lastName;
                }

                userManager.SaveChanges();
                profileManager.RecompileItemUrls(sfProfile);
                profileManager.SaveChanges();
            
            }

            return status;
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?