Thursday, March 28, 2013

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 pagefor 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 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    }

VB.NET CODE
01Protected Sub CreateUserWizard1_SendingMail(sender As Object, e As MailMessageEventArgs)
02 Dim newUserAccount As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)
03 Dim newUserAccountId As Guid = DirectCast(newUserAccount.ProviderUserKey, Guid)
04 Dim domainName As String = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
05 Dim confirmationPage As String = "/EmailConfirmation.aspx?ID=" & newUserAccountId.ToString()
06 Dim url As String = domainName & confirmationPage
07 e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url)
08 Dim smtp As New SmtpClient()
09 smtp.Host = "smtp.gmail.com"
10 smtp.Port = 587
11 smtp.UseDefaultCredentials = False
12 smtp.Credentials = New System.Net.NetworkCredential("YourGmailUserName@gmail.com", "YourGmailPassword")
13 smtp.EnableSsl = True
14 smtp.Send(e.Message)
15 e.Cancel = True
16End 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
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    }

VB.NET CODE
01Protected Sub Page_Load(sender As Object, e As EventArgs)
02 Dim newUserId As New Guid(Request.QueryString("ID"))
03 Dim newUser As MembershipUser = Membership.GetUser(newUserId)
04 If newUser Is Nothing Then
05  lblMessage.Text = "User Account not found"
06 Else
07  newUser.IsApproved = True
08  Membership.UpdateUser(newUser)
09  lblMessage.Text = "Account Approved, please <a href="" login.aspx""=""> Login</a> to continue"
10 End If
11End Sub

0 comments:

Post a Comment