Sitecore user:created event not fired on Membership.CreateUser

Sitecore have since 7.5’ish fired events each time you manipulate the users in the .NET Membership database:

<event name="user:created"></event>
<event name="user:deleted"></event>
<event name="user:updated"></event>
<event name="roles:usersAdded"></event>
<event name="roles:usersRemoved"></event>

But I noticed that the user:created event was not fired. This is because I call the .NET Membership provider directly:

string userNameWithDomain = "extranet\\myuser";
string password = "somepassword";
string email = "myuser@somewhere.com";
Membership.CreateUser(userNameWithDomain, password, email);

This call to Membership is not handled by Sitecore, thus no event is executed. To fix this I have found 2 solutions, one is not good and the other one is not good either.

SOLUTION 1: CALL THE SITECORE MEMBERSHIP PROVIDER DIRECTLY

This solution ignores the web.config settings and assume that you have not switched or overwritten the Membership Provider yourself. But it will fire the event though:

Sitecore.Security.SitecoreMembershipProvider provider = new Sitecore.Security.SitecoreMembershipProvider();
MembershipCreateStatus status = MembershipCreateStatus.Success;
provider.CreateUser(usernameWithDomain, password, email, "", "", true, null, out status);
if (status != MembershipCreateStatus.Success)
  throw new MembershipCreateUserException(status);

SOLUTION 2: RAISE THE EVENT YOURSELF

This solution requires you to raise the event yourself. You need encapsulate the call to create a user in your own class, and instruct everyone to never call Membership.CreateUser() directly:

MembershipUser user = Membership.CreateUser(usernameWithDomain, password, email);
Event.RaiseEvent("user:created", user);

I can see from other blog posts that the user events are not the most widely used events in the Sitecore toolbox. If you have found another solution to this problem please let me know.

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET, Sitecore 7, Sitecore 8 and tagged , , , , , , , , . Bookmark the permalink.

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