and modify CreateUser method again to store hashed password. Because we’re going to use user.passwordSalt property as an argument in CreatePasswordHash method we need to reorder the properties so the salt gets generated before the password is hashed:

I am aware that this is probably not the best method to hash the password and that the hashing and salt generating methods can be improved but this is not the point of this tutorial so I’ll leave it as it is.

The UserRepository.cs file up to this point in the tutorial can be downloaded here:

At this point we can run the application, register new account, log off and log in with it.

By the way, let’s tweak our application so it doesn’t log newly registered users in after the registration automatically.

We will want our users to activate the account by clicking the link in an email. Until activated user will not be able to log in (we will implement that in a second too).

Let’s create a View that will be displayed to users after the registration.

Open HomeController.cs file, and add Welcome method:

public ActionResult Welcome()
{
return View();
}

Right click on Welcome() and Add View…

In the View, add a message to newly registered users:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Welcome</h2>
<p>Thank you for registering!</p>
<p>Activation email has been sent to you. Click on the link in
the email to activate your account!</p>
</asp:Content>

Now, the activation key is generated we will email the activation link to the user.

For the purpose of this tutorial I will implement the email code directly into UserRepository class. In real world scenario you would ideally want to keep the email class separate. You would also want to add some sort of error handling for the email class.

You will also need an SMTP server that allows relaying messages. When you run the application on the Web Server from an ISP, usually it has SMTP role installed and you can use localhost as a server. This isn’t the case with Web Server installed with Visual Web Developer so you will need to find a server which will allow the application to send emails.

By the way, did you get the email verification working? I was wondering if I should include that due to hardly any servers allowing relaying and probably plenty of people following the tutorial on their home machines. In the end I thought it would be useful and you can always activate the account directly in the database (or create activated account)

Only part i did have trouble with was that .NET wouldn’t find the myMembershipProvider in the App_Data folder with the settings that you had. In the end i moved it back into the models folder and fully qualified it in web.config ie: CustomMembership.Models.MyMembershipProvider and then it worked fine.

I got the email verification working with no real issues (but I have an account setup for the purpose. It shouldn’t be an issue for most email systems anyway as long as they allow authentication).

A note to others implementing this (from experience) – don’t use Google as your email provider. Although you will get it working in test, when you move to production any volume of identical emails will get blocked.

I’d also like to see you move onto the Role provider as that’s where I’m scratching my head today.

Awesome tutorials. I have read many of them but didn’t see any interesting. Hope you’ll continue this and… tell us about more things we don’t know yet. Personally I’d read about changing password, editing in repository, complete ajax validation (f.e. user exists in database) and much more.

Hi!
I just would to congratulate you for this great tutorial. If anyone is using the lastest release of VS2010, no tutorial on the internet will fit but this. This tutorial is the only one that had worked 100% fine.

I’d be very thankful if someone could explain to me how to merge this Custom Membership Provider with Authentication and Authorization, allowing users to access the files only after loggin in.
I’m very noobie to MVC and I had being tryin , but I dont know how to associate it to this stuff.

Very helpful to me- im currently doing my final year project at university.. I modified it to work with my wcf service to connect to the database using entity framework there and had no issues at all..

I would also love to see your next section on roles (hopefuly before my handin date!)

This really was both extremely informative and easy to follow. I’m new to MVC, and after reading your articles in this series things are certainly much clearer. Thanks for the effort and time you’ve put into this and for making it freely available to others. Great work!

Hello Friends,
I also working on the custom membership using Entity framework.
It has almost 40 coloums.
I want to store that using cutom membership and also i want to create the ragistration page on MVC asp.net depends on these coloums.
Please help me to create UI in ASp.net MVC and as well the custom membership class to handle these coloums.

great tutorials ! I have got problems when registering new users. It throws the internal exception: “Cannot insert explicit value for identity column in table ‘Users’ when IDENTITY_INSERT is set to OFF.” at db.SaveChanges(); in userRepository.cs class. I have looked at code , there is not any value assigned to userid even though it’s throwing exception. WOuld please suggest me where I have change to set Identity_insert ON in visual studio. Please send me at email

This is Great nice article.
Where is The Definition Of AddToUser Method in UserReposotory.
If posible,Please send for me Full source code of this projects.
Thanks a lot.
I am Waiting so much………………………..