CreateUserWizard Account Activation Email Verification Confirmation

This example code is for CreateUserWizard Account Activation Through Email Verification Confirmation Or Validation In Asp.NET.

I am explaining how to create new signup using createnewuserwizard with membership provider and sending link to activate account using C# or VB.

CreateUserWizard Email Confirmation or verification
Read Create Log in Page Using Login Control to know how to setup membership provider.

I have created one NewUser.aspx page for signups.

One EmailVerification.aspx page to open when user clicks on the link in email sent to his emailid at the time of creating account.

Newly created accounts are deactivated by default and user won't be able to login untill he clicks on the link sent to his email id to validate, verify and activate.


First of all create a template which you want to send to users who sign up on the site. for this create a text file and write the text mentioned below and name it mail.txt.

Hello <%UserName%>!.

You or someone with your id signed up at this site, Your new account is almost ready, but before you can login you need to confirm your email id by visitng the link below:
<%VerificationUrl%>

Once you have visited the verification URL, your account will be activated.

If you have any problems or questions, please reply to this email.

Thanks!

Open NewUser.aspx page in design view and palce a CreateUserWizard control on it.

Set DisableCreatedUser property to true to deactivate new accounts untill user activate it by clicking the link.


Set MailDefinition property as mentioned below for wizard to send cenfirmation emails.

<MailDefinition From="YourGmailID@gmail.com" 
                Subject="Confirmation mail" 
                BodyFileName="~/mail.txt">
</MailDefinition>

HTML source of NewUser.aspx will look like
<form id="form1" runat="server">
<asp:CreateUserWizard ID="CreateUserWizard1" 
                      runat="server" 
                      DisableCreatedUser="True" 
        ContinueDestinationPageUrl="~/Login.aspx" 
        onsendingmail="CreateUserWizard1_SendingMail">
<MailDefinition From="YourGmailID@gmail.com" 
                Subject="Confirmation mail" 
                BodyFileName="~/mail.txt">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
</form>

Write code mentioned below in SendingMail event of CreateUserWizard control in code behind of page.

C# CODE
using System.Net.Mail;
using System.Web.Security;

protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
    {
        MembershipUser newUserAccount = Membership.GetUser(CreateUserWizard1.UserName);
        Guid newUserAccountId = (Guid)newUserAccount.ProviderUserKey;
        string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
        string confirmationPage = "/EmailConfirmation.aspx?ID=" + newUserAccountId.ToString();
        string url = domainName + confirmationPage;
        e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("YourGmailUserName@gmail.com", "YourGmailPassword");
        smtp.EnableSsl = true;
        smtp.Send(e.Message);
        e.Cancel = true;
    }

VB.NET CODE
Protected Sub CreateUserWizard1_SendingMail(sender As Object, e As MailMessageEventArgs)
 Dim newUserAccount As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)
 Dim newUserAccountId As Guid = DirectCast(newUserAccount.ProviderUserKey, Guid)
 Dim domainName As String = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
 Dim confirmationPage As String = "/EmailConfirmation.aspx?ID=" & newUserAccountId.ToString()
 Dim url As String = domainName & confirmationPage
 e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url)
 Dim smtp As New SmtpClient()
 smtp.Host = "smtp.gmail.com"
 smtp.Port = 587
 smtp.UseDefaultCredentials = False
 smtp.Credentials = New System.Net.NetworkCredential("YourGmailUserName@gmail.com", "YourGmailPassword")
 smtp.EnableSsl = True
 smtp.Send(e.Message)
 e.Cancel = True
End Sub

Mail sent will look like shown below.
Createuserwizard verification email

To activate user through EmailConfirmation.aspx page Place a label control on the page and write below mentioned code in Page_Load Event.

C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        Guid newUserId = new Guid(Request.QueryString["ID"]);
        MembershipUser newUser = Membership.GetUser(newUserId);
        if (newUser == null)
        {
            lblMessage.Text = "User Account not found";
        }
        else
        {
            newUser.IsApproved = true;
            Membership.UpdateUser(newUser);
            lblMessage.Text = "Account Approved, please  Login to continue";
        }
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 Dim newUserId As New Guid(Request.QueryString("ID"))
 Dim newUser As MembershipUser = Membership.GetUser(newUserId)
 If newUser Is Nothing Then
  lblMessage.Text = "User Account not found"
 Else
  newUser.IsApproved = True
  Membership.UpdateUser(newUser)
  lblMessage.Text = "Account Approved, please  Login to continue"
 End If
End Sub


Build and run the application.

Download Sample Code



If you like this post than join us or share

11 comments:

sandeep shah said...

smtp.Send(e.Message)

error at above line


Unknown said...

@sandeep: What error you are getting ?


Puneet Soni said...

string confirmationPage = "/EmailConfirmation.aspx?ID=" + newUserAccountId.ToString();



This line of code may produce some "Application'/'error". So to correct this the code must be:

string confirmationPage = "EmailConfirmation.aspx?ID=" + newUserAccountId.ToString();


The above code worked for me.


Anonymous said...

This comment has been removed by a blog administrator.


Anonymous said...

This comment has been removed by a blog administrator.


Anonymous said...

this code can not be working the error is occured on login page.


-Edwin- said...

how to generate the random code if im not using createuserwizard? is there any alternative?


TC said...

For VB Users, you will need the line

Imports System.Net.Mail

At the top of the VB Code-behind.

This is a great article, found it very useful - thanks!


Anonymous said...

Thanx alot.....!!!!!!


Anonymous said...

Sir, i havn't receive any mail ...


Unknown said...

I am not getting the link in mail?


Find More Articles