CreateUserWizard Email Verification Or Confirmation In Asp.NET.
In this example i am explaining how to create new user using createnewuserwizard with membership provider and sending verification or confirmation link in Email using C# or VB in ASP.NET.
Read Login Page Using Login Control to know how to create login page in asp.net and setup membership provider to use with login control.
For this example i have created one NewUser.aspx page to place createuserwizard on it to create new users or signups.
One EmailVerification.aspx page to open when user clicks on the link in email sent to his emailid at the time of creating new account.
This page will verify,confirm or validate new user account and activate it. when new account is created through wizard, it will be deactivated and user won't be able to login untill he clicks on the link sent to his email id, clicking on the link activates new account.
First of all create a email template which you want to send to users who sign up or create new account onthe site. for this create a text file and write the text mentioned below and name it mail.txt.
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 sent in email.
Set MailDefinition property as mentioned below for wizard to send cenfirmation emails.
Write code mentioned below in SendingMail event of CreateUserWizard control in code behind of page.
Email sent to new user's email id provided in email section of wizard will look like shown below.
To activate user account through EmailConfirmation.aspx page Place a label control on the page and write below mentioned code in Page_Load Event.
Build and run the application.
In this example i am explaining how to create new user using createnewuserwizard with membership provider and sending verification or confirmation link in Email using C# or VB in ASP.NET.
Read Login Page Using Login Control to know how to create login page in asp.net and setup membership provider to use with login control.
For this example i have created one NewUser.aspx page to place createuserwizard on it to create new users or signups.
One EmailVerification.aspx page to open when user clicks on the link in email sent to his emailid at the time of creating new account.
This page will verify,confirm or validate new user account and activate it. when new account is created through wizard, it will be deactivated and user won't be able to login untill he clicks on the link sent to his email id, clicking on the link activates new account.
First of all create a email template which you want to send to users who sign up or create new account onthe 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 email 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!
You or someone with your email 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 sent in email.
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
Email sent to new user's email id provided in email section of wizard will look like shown below.
To activate user account 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 SubBuild and run the application.



