Calling Membership.CreateUser with DB transaction support

The Membership.CreateUser method does not have an overload where you can specify the database transaction your code is using. I’m using ASP.NET and the .NET Framework 3.5 .

To accomplish this, you can do one of two things:

  1. You can use the Distributed Transaction Coordinator (DTC) via .NET’s TransactionScope class. http://msdn.microsoft.com/en-us/library/ms172152.aspx
  2. Or you can call the aspnet_Membership_CreateUser stored procedure directly; which is ultimately what the Membership.CreateUser method does.

Either option works well but if you’re not using DTC for anything else in your application I think it is an overkill to use it just for that. DTC needs to be running on the machine where the code is running (where IIS is running) and it also must run on the machine where the database is running. Both machine’s DTC settings need to be configured and if there is a firewall between the machines, you must poke some holes in it.

If you decide to go with option #2, you can call the stored proc using Linq or the SqlCommand class. Either way will allow you to specify a transaction. Just make sure that you check on the stored procedure’s return value. It should be zero when no errors occur. Otherwise, cast the value to the MembershipCreateStatus enum and that will tell you what happened.