2

I have just switch to .Net membership provider to manage my users. I have a table relating to the Users table called UserGroupDetails. I would like to add a row to this table when Membership.CreateUser is called. If adding the user fails i would like to make sure that the record in UserGroupDetails is removed or not added and if adding the record to UserGroupDetails fails i would like the Membership.CreateUser to fail and the user to not be added.

I am not really interested in using the SqlProfileProvider and i have not found any information about overriding or modify the Membership.CreateUser to perform this additional insert.

Is it possible and if so could someone point me to a good resource on how to accomplish this?

Also, i do not want to rewrite the whole Membership.CreateUser method. Rather i would like to simple add to it. I have looked at http://msdn.microsoft.com/en-us/library/ms366730.aspx but this seems to be rebuilding the wheel.

Thanks, D

Nugs
  • 5,503
  • 7
  • 36
  • 57

4 Answers4

1

You would build a custom provider

TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
1

Create a custom provider that inherits membershiprovider. Override the create user method to extend the functionality. Call base.CreteUser and then add your code for adding the additional records to your custom table.

UPDATE:

Here's what your custom provider would look like

using System.Web.Security;

namespace YourNameSpace
{

    public class CustomSqlMembershipProvider : SqlMembershipProvider
    {

        public bool CreateUser(//list of parameters)
        {
            bool UserCreated = false;
            MembershipCreateStatus status;

            //provide the required parameters 
            base.CreateUser(//list of parameters);

            if (status == 0)//user was successfully created
            { 
                //perform your custom code
                //if success 
                UserCreated = true;
            }

            return UserCreated
        }
    }
}

Make sure you reference your new custom provider in your Web.config file

   <membership defaultProvider="CustomMembeshipProvider">
      <providers>
        <clear/>
        <add connectionStringName="Your connection string name" name="CustomMembeshipProvider"
             type="YourNameSpace.CustomSqlMembershipProvider" />
      </providers>
    </membership>
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
gsirianni
  • 1,334
  • 2
  • 18
  • 35
1

I've done this! I start by creating my aspnet user first. Here's what I'm doing.

1) I added this to my web.config:

<system.web>
        <membership>
            <providers>
                <clear/>
                <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
                     enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
                     maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
                     applicationName="[ApplicationName]" />
            </providers>
        </membership>
        <machineKey validationKey="[]" decryptionKey="[]" decryption="3DES" validation="SHA1" />
 <connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=(local);Initial Catalog=[DataBaseName];User ID=[UserName];Password=Password" providerName="System.Data.SqlClient" />
 </connectionStrings>
 </system.web>

2) I then use this to create my user table

   var user = Membership.CreateUser(request.UserName, request.Password, request.Email);

3) Finally I grab my aspnet user id I just created and relate it in my custom user

user.ProviderUserKey.ToString();

You could create some logic to delete the aspnet user if insert into UserGroupDetails fails.

Eric Walter
  • 875
  • 1
  • 8
  • 17
1

You don't need to build a custom provider. Just add your code to hookup your UserGroupDetails table after you call CreateUser.

Unfortunately, you can't use a TransactionScope to rollback the transaction if something fails because the CreateUsre will use a seperate database connection from your code, and this will require promoting the transaction to a distrivuted transaction, which is a pain to configure and setup.

The easiest solution is just check for failure of the CreateUser, and don't execute the second bit of code if it fails, and deleting the created user if your second bit fails. However, this is not foolproof as there are situations where you might have an orphaned record.

In order to do this in a single transaction, then the only solution is a custom provider, but I personally don't think it's worth the effort.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291