| Details |
![]() |
| QuickDocId | 569 |
| Written by | Imar Spaanjaars |
| Posted | 07/18/2012 14:04 |
| Page views | 7600 |
Are you looking to hire an experienced software developer or .NET consultant? Then get in touch with me through my company's web site at devierkoeden.com
Found an interesting article on this site? Got inspired by something you read here? Then consider making a donation with PayPal.
Over the past couple of weeks I received a number of e-mails from readers with more or less the same question: how do you require users to confirm their e-mail addresses before they are allowed to log in with an account they just created. Rather than answer them individually by e-mail, I decided to write this article to explain the principles.
In this article I’ll show you how you can let users sign up for an account that they need to confirm first before they can sign in with it. By using the standard ASP.NET features for account management (Membership, the CreateUserWizard control, built-in e-mail sending capabilities), it turns out this is pretty easy to do. Here’s a short rundown of all the required steps:
I’ll dig a little deeper into each step in the following sections. You’ll find the full source code for the demo application at the end of the article. This article was written using Visual Studio 12 and SQL Server LocalDB. However, if you follow the instructions and create a new site from scratch, it would equally work in Visual Studio 2008 and 2010.
The first thing you need to do is modify your site so users can sign up for an account. This part is pretty simple and is identical to what you would normally do. I won’t describe this part in detail but instead describe the steps I followed to set up the demo site.
Install-Package Microsoft.AspNet.Providers
Dear <% UserName %> Your account has been created. Please find your account details below: Username: <% UserName %> Password: <% Password %>
This file is saved as ~/App_Data/YourNewAccount.txt in the sample application.
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" >
<MailDefinition BodyFileName="~/App_Data/YourNewAccount.txt" From="imar@example.com" Subject="Your new account">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
This step is really easy because the CreateUserWizard has built-in support for this. All you need to do is set DisableCreatedUser to True, like this:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" DisableCreatedUser="True">
With this setting on, you should no longer be able to log om with new new accounts you create on the site.
This action consists of a few steps:
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
ProfileCommon userProfile = ProfileCommon.Create(CreateUserWizard1.UserName) as ProfileCommon;
accountConfirmationKey = Guid.NewGuid().ToString();
userProfile.AccountConfirmationKey = accountConfirmationKey;
userProfile.Save();
}
<profile defaultProvider="DefaultProfileProvider">
<properties>
<add name="AccountConfirmationKey"/>
</properties>
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
{
string confirmLink =
string.Format("{0}://{1}/Confirm.aspx?ConfirmationKey={2}&UserName={3}",
Request.Url.Scheme, Request.Url.Authority, accountConfirmationKey,
CreateUserWizard1.UserName);
e.Message.Body = e.Message.Body.Replace("##ConfirmLink##", confirmLink);
}
Dear <% UserName %>, Your account has been created. However, before you can use it you need to confirm your e-mail address first by clicking the following link: #ConfirmLink## Please find your account details below: Username: <% UserName %> Password: <% Password %>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" DisableCreatedUser="True"
OnCreatedUser="CreateUserWizard1_CreatedUser" OnSendingMail="CreateUserWizard1_SendingMail"
CompleteSuccessText="Your account has been successfully created. You'll
receive an e-mail with a confirmation link that you need to click to confirm
your e-mail address and activate your account."
>
<MailDefinition BodyFileName="~/App_Data/YourNewAccount.txt"
From="imar@example.com" Subject="Your new account">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>

When the user clicks the confirmation link in the e-mail message, he's being sent to the Confirm.aspx page, along with the confirmation ID and UserName in the query string. A typical link could look like this:
http://localhost:6864/Confirm.aspx?ConfirmationKey=ef05127f-a050-41b9-9132-be8a8aeea925&UserName=Imar
The localhost:6864 part varies depending on where you run this code, and would include your full domain name when used on a production site.
In the code behind of Contfirm.aspx, you then get the user name from the query string and use it to create a Profile instance. This profile instance should have a value for the AccountConfirmationKey property that matches the confirmation key sent in the query string. If the two match, you can create a MembershipUser for the requested user name and activate the account by setting IsApproved to true. Here’s the code from the sample application that shows a way you can implement this:
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.QueryString.Get("UserName");
string accountKey = Request.QueryString.Get("ConfirmationKey"); var profile = ProfileCommon.Create(userName) as ProfileCommon; if (accountKey == profile.AccountConfirmationKey) { var user = Membership.GetUser(userName); user.IsApproved = true; Membership.UpdateUser(user); Status.Text = "Account confirmed successfully."; } else { Status.Text = "Something went wrong while confirming your account."; } }
As you can see, implementing this functionality is pretty straightforward, as most of what you need is supported out of the box. Hooking into some events to customize behavior is a great way to make modifications to existing functionality without building your own security system from scratch.
The sample application that comes with this article is pretty simple so it’s easy to focus on the concept without getting confused by too many details. For a real-world website, you may want to implement the following features:
Wonder where to go next?
You can read existing comments below
or you can post a comment yourself on this article
.
Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.
For more information about the Talk Back feature, check out this news item.
| QuickDocId | 569 |
| Full URL | http://imar.spaanjaars.com/569/requiring-users-to-confirm-their-e-mail-address-after-they-create-an-account |
| Short cut | http://imar.spaanjaars.com/569/ |
| Written by | Spaanjaars.Imar.Model.ContentSummary.CreateAuthorName |
| Date Posted | 07/18/2012 14:04 |