Create users
When creating a user, you must perform the following steps:
- Get instances of the user and user profile managers.
The managers are represented by theUserManagerandUserProfileManagerclasses. For more information about getting their instances, see Managing users. - Create the user.
To create the user call theCreateUsermethod of theUserManagerclass and pass the required arguments. TheCreateUsermethod accepts an output parameter of typeSystem.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. - Check whether the user has been created successfully.
If the user has been created successfully, the value of the output parameter must be equal toMembershipCreateStatus.Success. - 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 theCreateProfilemethod of theUserProfileManagerclass and pass the required parameters. For the type of profile you useSitefinityProfile. For more information, see For developers: User profiles. - Save the changes.
Finally, you save the changes to bothUserManagerandUserProfileManager.
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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.