Login controls are just great. They simplify so many common tasks for you. In this article I will show you that how you can create a Custom Membership Provider which will save the data into your own custom database. I will implement the CreateUser method which will create a new user in the database.

Introduction:

Login controls are just great. They simplify so many common tasks for you. In this article I will show you that how you can create a Custom Membership Provider which will save the data into your own custom database. I will implement the CreateUser method which will create a new user in the database.

Creating a Custom Membership Provider Class:

The first thing that you need to do is to create a class that inherits from the MembershipProvider class. After you inherit you need to override several methods. In this article I will just override the CreateUser method which is used to create a new user. I am using an entity class to save my data but you can use any approach you want. Here is the implementation of the CreateUser method.

public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

{

bool result = false;

MembershipUser user = new MembershipUser(Name, username, providerUserKey,

email, passwordQuestion, null, isApproved, false, DateTime.Now, DateTime.Now,

DateTime.Now, DateTime.Now, DateTime.Now);

User newUser = new User(username, password, email);

result = newUser.Save();

if (result)

{

status = MembershipCreateStatus.Success;

return user;

}

else

{

status = MembershipCreateStatus.ProviderError;

return null;

}

}

In the CreateUser method I am making a new instance of the MembershipUser class. After the instance has been created I send the username, password and the email to create a new user instance and later I used the Save method of the user class to insert the user in the database. Since the status variable is marked with out keyword which means that you have to change its value before sending it back. If the user is created a true value is sent back and the MembershipCreateStatus.Success is assigned to the status variable else MembershipCreateStatus.ProviderError is assigned.

After making the custom membership class you need to add few settings in the web.config so that you will be able to use your newly created created Custom Membership Provider.

Settings in Web.config:

The first thing that I need to do is to make the connection string. Here is the code that makes up the connection string:

<connectionStrings>

<add name="ConnectionString" connectionString="Server=localhost;Database=MyDatabase;Trusted_Connection=true"/>

</connectionStrings>

After making the connection string I need to make the settings for the membership section since I will be using my own membership section.

<membership defaultProvider="MyCustomMembershipProvider">

<providers>

<add name="MyCustomMembershipProvider"

connectionStringName="ConnectionString"

passwordFormat="Hashed"

minRequiredPasswordLength="2"

minRequiredNonalphanumericCharacters="0"

requiresQuestionAndAnswer="False"

requiresUniqueEmail="True"

type="MyCustomMembershipProvider"/>

</providers>

</membership>

The name of the provider is "MyCustomMembershipProvider". The type will be the name of your custom class which in this case is MyCustomMembershipProvider.

After making the settings in the web.config you are good to use your new custom made membership provider.

Using the CreateUserWizard Control to Create New Users: 

Simply drag and drop the CreateUserWizard control on the form. If you want to customize it then I suggest that you take a look at my article "Customizing CreateUserWizard Control". And that's pretty much it! I know you must be wondering that I did not write any code to send the values from the CreateUserWizard control to the CreateUser method of your custom membership provider. Well, you don't have to since you have already made settings in the web.config so the control knows what method to look for and where is it located.

Don't try to manually fire the CreateUser method by using the following code:

Membership.CreateUser("azamsharp", "mypassword", "something@gmail.com");

The reason is that when you manually fire it and add a new user. It will add two entries in the database table. One will be added because you fired the CreateUser and one will be added since the CreateUser is fired internally by the CreateUserWizard control.

 

If you want to insert some other fields like First Name, Last Name etc then maybe you should use the Wizard control instead of the CreateUserWizard control.

Please feel free to download the the source code at the end of this article.

I hope you liked the article! happy coding.

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW