Wednesday 29 May 2013

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.

Read Create Log in Page Using Login Control to know how to setup membership provider.

I have created one NewUser.aspx pagefor signups.

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

Newly created accounts are deactivatedby 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
01using System.Net.Mail;
02using System.Web.Security;
03 
04protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
05    {
06        MembershipUser newUserAccount = Membership.GetUser(CreateUserWizard1.UserName);
07        Guid newUserAccountId = (Guid)newUserAccount.ProviderUserKey;
08        string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
09        string confirmationPage = "/EmailConfirmation.aspx?ID=" + newUserAccountId.ToString();
10        string url = domainName + confirmationPage;
11        e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url);
12        SmtpClient smtp = new SmtpClient();
13        smtp.Host = "smtp.gmail.com";
14        smtp.Port = 587;
15        smtp.UseDefaultCredentials = false;
16        smtp.Credentials = new System.Net.NetworkCredential("YourGmailUserName@gmail.com", "YourGmailPassword");
17        smtp.EnableSsl = true;
18        smtp.Send(e.Message);
19        e.Cancel = true;
20    }

Mail sent will look like shown below. 

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
01protected void Page_Load(object sender, EventArgs e)
02    {
03        Guid newUserId = new Guid(Request.QueryString["ID"]);
04        MembershipUser newUser = Membership.GetUser(newUserId);
05        if (newUser == null)
06        {
07            lblMessage.Text = "User Account not found";
08        }
09        else
10        {
11            newUser.IsApproved = true;
12            Membership.UpdateUser(newUser);
13            lblMessage.Text = "Account Approved, please <a href="\"Login.aspx\""> Login</a> to continue";
14        }
15    }

Build and run the application.

No comments:

Post a Comment