Sitecore Custom User Profile Properties

Image

I've found myself using custom user profile properties on a few projects that use Sitecore authentication. I've used it to keep track of Sitecore user accounts that require a password reset after forgot password has been called and an email sent to the user. I've added custom profile properties to keep track of the last time a page with a specific item template has been called. I would like to share how to do this.First thing you will need to do is log into the Sitecore Desktop and switch to the Core database. Open the Content Editor and navigate to the /sitecore/templates/System/Security/User node. Now use the builder tab to add the fields of your choice:

scCustUserProp1

In my example, I've added "Has Password Reset" to handle one of the examples described above and "Last News View" to handle the other example.If we now navigate to /sitecore/system/Settings/Security/Profiles/User, we can see that our user profile has these properties.

scCustUserProp2

As a side note, On yet another project in the past, I have created a custom user profile template that inherited from /sitecore/templates/System/Security/User, added my custom properties to my new custom profile template under a Data field grouping, then created a new profile under /sitecore/system/Settings/Security/Profiles. Let's call it Custom User for this example. Then when I programatically created users, I've set their user Profile by calling:[code language="csharp"]User u = User.FromName("sitecore\\myNewUser", true);u.Profile.ProfileItemId = "{11111111-1111-1111-1111-111111111111}";// Item ID String in Core DB of /sitecore/system/Settings/Security/Profiles/Custom User[/code]Now that we have our custom user profile properties, let's use them. After a user submits their email, an email is sent to the user with a temporary password. I can set the custom property so our application knows that forgot password was triggered.[code language="csharp"]MembershipUser _user = Membership.GetUser();Sitecore.Security.Accounts.User securityAccountUser = Sitecore.Security.Accounts.User.FromName(_user.UserName, true);if (securityAccountUser != null){ // Set the value to "1" since we are using a Sitecore CheckBox Field securityAccountUser.Profile.SetCustomProperty("Has Password Reset", "1"); securityAccountUser.Profile.Save();}[/code]Once the user logs into my Sitecore web application. I can check if the user has a password reset by calling:[code language="csharp"]string resetValue = Sitecore.Context.User.Profile.GetCustomProperty("Has Password Reset");if (resetValue == "1" && Sitecore.Context.User.IsAuthenticated){ // redirect to change password page.}[/code]Once the user has successfully changed their password, we can then set our custom property back to null:[code language="csharp"]MembershipUser _user = Membership.GetUser();Sitecore.Security.Accounts.User securityAccountUser = Sitecore.Security.Accounts.User.FromName(_user.UserName, true);if (securityAccountUser != null){ securityAccountUser.Profile.SetCustomProperty("Has Password Reset", null); securityAccountUser.Profile.Save();}[/code]To handle the last view of a news page, I used the Profile SetCustomProperty and GetCustomProperty to save a query string that I convert back and forth from a NameValueCollection to keep the last time they visited that Item. The Item ID is the key and the value is the time the page was visited.