Creating an extended Sitecore user for your site

You might want to use the sitecore ASP.NET membership provider Core database to manage users for your sitecore site.

In that case, at some point during your site development, you will come across the need to create a new sitecore user from your site front end.
You might also want to have additional fields as is relevant to your application.
In this example, we are adding user address as additional custom fields.

The user template is available in the Core database of your sitecore instance.
The template is available at: /sitecore/templates/System/Security/User

2014-06-15_011504

You might also want to add a custom status field to be used by a site administrator – for user profile approval.
2014-06-15_012247

This field could point to a list of options also available in the core database:
Sample Template: /sitecore/templates/System/Security/User Profile Data/Profile Status Type
2014-06-15_013127

Options: /sitecore/content/Home/Data/Profile Status Types
2014-06-15_013211

Following is the code you could use to create the user:

Please note the code for default selection of the drop down field in the user template, and also the default ProfileItemId – which will determine which view the user will open in when you access the user from the User Manager console.

public bool CreateUpdateAccount(string firstName, string lastName, string emailAddress, string password,
            string addressLine1, string addressLine2, string city,
			string ddlCountriesText, string ddlStateText, string postalCode)
{
	var formData = new NameValueCollection();
	Sitecore.Security.Accounts.User user = null;

	formData["Name"] = firstName + " " + lastName;
	formData["Email"] = emailAddress;
	formData["Password"] = password;

	user = SecurityHelper.SitecoreSecurity.CreateSitecoreUser(formData);

	if (user != null)
	{
		Sitecore.Security.Authentication.AuthenticationManager.Login(user);

		user.Profile.SetCustomProperty(Constants.UserCustomProperty.AddressLine1, addressLine1);
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.AddressLine2, addressLine2);
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.City, city);
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.Country, ddlCountriesText);
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.Region, ddlStateText);
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.PostalCode, postalCode);

		// Constants.CoreDb.ItemGuid.PendingApproval - contains the GUID of the Pending Approval item in Core DB
		// in the folder: /sitecore/content/Home/Data/Profile Status Types
		user.Profile.SetCustomProperty(Constants.UserCustomProperty.ProfileStatus, Constants.CoreDb.ItemGuid.PendingApproval);

		// Constants.CoreDb.ItemGuid.PendingApproval has the guid of the item in Core at: /sitecore/system/Settings/Security/Profiles/User
		// This is to ensure that the default mode of the site created user has these custom properties.
		user.Profile.ProfileItemId = Constants.CoreDb.ItemGuid.SecurityProfileUser;
		user.Profile.Save();

		return true;
	}
	return false;
}

Security Helper method:

public static User CreateSitecoreUser(NameValueCollection formData)
{
	var fullUsername = Sitecore.Context.Domain.GetFullName(Guid.NewGuid().ToString());
	var user = User.Create(fullUsername, formData["Password"]);

	using (new SecurityDisabler())
	{
		// Constants.CoreDb.ItemGuid.PendingApproval has the guid of the item in Core at: /sitecore/system/Settings/Security/Profiles/User
		user.Profile.ProfileItemId = Constants.CoreDb.ItemGuid.SecurityProfileUser;
		user.Profile.FullName = formData["Name"];
		user.Profile.Email = formData["Email"];
		user.Profile.Save();
	}
	return user;
}

Following is a user as seen from the user manager which was created from a site (note the ‘extranet’ domain)
2014-06-23_215757

2014-06-23_220039

Here is the populated droplink field:
2014-06-23_220100

For user login, the following code can be used:

Sitecore.Security.Authentication.AuthenticationManager.Login(domainName + @"\" + userName, password, false);

To validate user:

System.Web.Security.Membership.ValidateUser(domainName + @"\" + userName, password));

To access the custom fields:

var address1= currentUser.Profile.GetCustomProperty(Constants.UserCustomProperty.AddressLine1);

You can also get the list of users pending approval (if you wanted to for a custom User Admin page):

    public static List<User> GetUsersPendingApproval()
    {
		IFilterable<User> allUsers = UserManager.GetUsers();

		// Constants.CoreDb.ItemGuid.PendingApproval - contains the GUID of the Pending Approval item in Core DB
		// in the folder: /sitecore/content/Home/Data/Profile Status Types
		return allUsers.Where(user => user.Profile.GetCustomProperty("Profile Status") == Constants.CoreDb.ItemGuid.PendingApproval).ToList();
    }
Advertisements

6 thoughts on “Creating an extended Sitecore user for your site

    • Thanks Mike, Looks like I did add in additional information about adding drop down fields / verifying user / login / get pending users, but I did see some of these in another blog by Brian now. Thanks for the heads up though.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s