Problem
I’ve received several requests to update my original post dealing with adding a confirmation step to the standard ASP.NET MVC registration process so that it can work with ASP.NET MVC3. This seemed like a perfect place to use (and learn) Nuget, so you don’t have to deal with all the steps of creating the methods, modifying the views, etc.
Solution
For now, I’m going to assume that you already have Nuget installed and you have some idea of how to use it. If you don’t, read through the instructions at the Nuget site, or check out blog posts by Scott Hanselman or Phil Haack. If you still need help, post something in the comments and I’ll expand this post to include a Getting Started section.
Step 1a – Create a New Project
In Visual Studio, create a new ASP.NET MVC 3 Internet Application.
Step 1b – Work with an Existing Project
This will work with an existing project, but I’m assuming that you’re using default everything. If you’ve modified the AccountController or AccountModels in an uber custom way, you’ll likely experience pain. If you’re using out-of-the-box functionality, you won’t have any issues. And by “won’t”, I mean you’re completely on your own.
Step 2 – A little preparation
Now that you got your project, rename Controllers/AccountController.cs and Views/Account/LogOn.cshtml by adding “.old” to each file.
This needs to be done because Nuget cannot modify existing files (or at least I don’t know how to do it). These files are included in the Nuget package, and the default behavior of Nuget is to ignore any files that it finds on disk that match files in the package. By renaming them, if you have any custom code in there, you won’t lose it – you’ll just need to port it over to the new files.
Step 3 – Installing the package
A. From the console:
If you’re a fan of the console, Select Tools | Library Package Manager | Package Manager Console. At the console prompt type: install-package MvcConfirmationEmail. Nuget will download the package and modify your project accordingly.
B. From the User Interface:
Right-click on your project root, and select “Manage Nuget Packages…”. When the dialog window pops-up, enter “ConfirmationEmail” (one word) in the search text box in the upper right corner.
After a few seconds, it will find the MvcConfirmationEmail package (shown above). Click the install button, and it will download and install the package into your project.
WARNING!
If you didn’t rename the files as noted in Step 2 – this won’t work, and you will need to uninstall the package (PM>uninstall-package MvcConfirmationEmail), rename the files, and do this again.
Step 4 – Update web.config
When Nuget installed the package, a few additions were made to web.config so that you can configure your email service provider’s settings.
A. Email From Name
When you send an email to someone during the registration process, it has to come from a named account. Open up web.config and find the configuration\appSettings section. There is a key named MvcConfirmationEmailFromAccount. Change the value to the account name that will be sending the email. I’ve been using Gmail as my test account and it works great.
B. Mail Settings
You need to configure the settings for your email service provider. Head down to the bottom of the config file, and you’ll find the system.net\mailSettings\smtp node. Set the appropriate values for host, userName, password, and port, Gmail requires port 587 for SMTP – if you’re not using Gmail – you will need to check with your service provider to see what this should be.
That’s it! You should now be able to run the application and have the same functionality/workflow available in the original post. If you run into any problems or have any questions, please post down in the comments section and I’ll be happy to walk through your issues.
UPDATE 10/18/2011: I created a sample site on AppHarbor that you can use to test the functionality/workflow. http://confirm.apphb.com.
{ 16 comments… read them below or add one }
This was exactly what I was looking for:
Everything is working for me except when I click on the confirmation link in the perspective users recieved email, it returns in the browser as “could not connect to mypage.apphb.com:14978″.
As I am hosting on AppHarbor, could this may be the reason? Any pointers would be very much appreciated.
Many thanks for the turtorial Jc
JC -
Hmmm… I haven’t tried this with appharbor yet, but it should work. If you’d like I could send you an email and we could do more troubleshooting off-line.
Regards,
Kevin
Hi Kevin,
Yes please, that would be terrific
Thank you v. much
Jc
JC –
I just created a test app on appharbor and I got the same error. When the confirmation url is built, it includes a port number… so the confirmation link I received was “http://confirm.apphb.com:14715/account/verify?ID=0e6a77f6-ab9d-4530-b253-76673da76ded”. When I removed the port number I ended up with http://confirm.apphb.com/account/verify?ID=0e6a77f6-ab9d-4530-b253-76673da76ded – and that WORKED. I need to look at the code that’s building the url and make sure it’s not including the port.
For now, you can got into the SendConfirmationEmail method and change the line:
string verifyUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + “/account/verify?ID=” + confirmationGuid;
TO:
string verifyUrl = “http://mypage.apphb.com” + “/account/verify?ID=” + confirmationGuid;
WHERE: “http://mypage.apphb.com” is the name of your appharbor application. Mine would be “http://confirm.apphb.com”.
This isn’t ideal (as it won’t work locally)… and I’ll dig into it more tonight.
Regards,
Kevin
JC –
Based on this support note on AppHarbor http://support.appharbor.com/kb/getting-started/workaround-for-generating-absolute-urls-without-port-number I just upgraded the package to v1.1. You can either create a new project and run ‘Install-Package MvcConfirmationEmail’ – or run ‘Update-Package MvcConfirmationEmail’ in a v1.0 project. Thank you for pointing this out! I tested that it works locally, as well as when deployed to AppHarbor. Please let me know if you have any questions.
Regards,
Kevin
Hi Kevin,
You are a star. I simply updated to 1.1 and bobs your uncle.
Brilliant brilliant brilliant. I would have never figured that out.
Brilliant
Thanks again JC
One approach that I’m trying out that could be useful for you is to implement AccountController as a partial class, then you only need to add the partial keyword and remove excess methods from the existing controller, whilst including the controller methods for this as AccountController.MvcConfirmation.cs.pp (NB: use the substitutions to match the namespace) instead of calling it AccountController.cs.
Is this nuget still there? It didn’t come up in my search.
ProgTick -
It’s there… if you go to the Nuget Package Manager Console, you can type in “PM> Install-Package MvcConfirmationEmail” (minus the quotes). The search on nuget.org is not very good – but it’s there… http://nuget.org/list/packages/mvcconfirmationemail.
I updated the tags on the package… so you should now search for ConfirmationEmail (no spaces)… and their indexing will find it better. Thanks for finding this issue!
Regards,
Kevin
I tried to register myself on your test app and I just get the following msg,
“Sorry, an error occurred while processing your request.”
I also did not receive a confirmation email. Do you have any idea what’s going on?
thanks,
Yung
Hi Yung -
Sorry about that! My password had expired… so I created a new gmail account and reset the credentials. I just tested it and it’s working now. I deleted your email address from the server so you should be able to retry it now. I guess I should pay more attention to this
.
Regards,
Kevin
Hi Kevin,
I’m using your approach with MVC 4 Beta and I got a bad problem. When I register with my app the confirmation email is sent and all is working well. When I register a new user with the same email address theconfirmation mail isn’t sent at all, eventhough it passes the client.Send(message) in debug without any errors. Why am I not able to test the functionality twice with the same email address?
Regards,
Vincenz
Well, thats embarrassing, it was all my junk email folder.
Hi Vincenz – been there, done that! Glad you’ve got it working. Any issues with MVC4?
This worked out really well. Thanks! Now I’m trying to add the ChangePassword behavior. I’m still learning this, so I’ll bang my head on it a while, but if anyone wants to post their solution, I’d appreciate it.
Where is the phrase “Thank you for registering. Please check your email for a confirmation message with a link that will confirm your account. Once you click the link, your registration will be complete.” buried in the code? Has to be obvious but I can’t find it.
{ 1 trackback }