Want to learn how to write Asp.net code for sending activation links to new registered members on a website. This is about using ASP.NET membership feature and sending Email to newly registered Member a email to confirm link.
Lets begin with the Registration Feature of ASP.NET membership that will send Email with confirmation code or Activation link to the newly registered email id.
When user will click link, then account will be active according to the code or link we have send to the email id.
Lets begin with the assets we have:
Visual Studio 2012 Ultimate
Windows 8 Pro
MS Sql Server 2008 R2
C# for Backend Language
I am here assuming that you have the basic knowledge of ASP.NET.
Open Visual Studio and Click on New >> Website and Choose Empty Website in your Required Framework version in Required Language (vb or c#).
Right Click on Solution Name and Click Add Item and Choose Web Form.
We need at least below Pages and Items in Solution.
Home Page (Default.aspx)
Login Page (Login.aspx)
Registration (Register.aspx)
Confirm Page (Confirm.aspx)
Link for Mail (ConfirmationCode.html)
Additional forms and Folders as per requirement.
What we will do in above forms.
In Default.aspx we will put a Login View (Login/Logout) LoginName control.
In Login.aspx we are putting a Login Control in this Page.
In Register.aspx we will put a CreateUserWizard control to register.
In Confirm.aspx we will put some code in Backend and accept the Confirmation Code based in URL, or Entered Code.
In ConfirmationCode.html we will put Generated Code for Sending Mail. This will be attachment of Mail Body.
Open your Toolbox of Visual Studio and Insert a CreateUserWizard from Login Section in Form according to your Design as mentioned above and other designs.
web.config
We need to add some note in web.config file. Here is the Code that is required in web.config.
ConfirmationCode.html
Lets Type the Body of HTML Message that will be send on Registration.
Register.aspx
Open the Properties of Registration Control and by Pressing F4 after selecting Control or Right Click and Choosing Property.
Also set MailDefinition for sending mails and Define attachment file.
Now Right Click on CreateUserWizard Control and Choose Event after selecting Property window.
From the Events write this code on SendingMail Event.
Lets see the Code for Sending Mail.
Namespaces Required:
System.Net for Sending Mail and System.Web.Security to access Membership features.
Description of Above Code:
Line 1: We get the Username of Currently Created User.
Line 2: We get UserKey of currently Created User and converted it to GUID Format.
Line 3: We find name of Domain where it is being Hosted. Only Domain (Left Part before / and Application path if any).
Line 4: We put path to Confirm page on a String variable to join with Domain.
Line 5: We joined Confirmation Link and Domain Name to form a Complete URL.
Line 6: We Replaced Keywords of Mail Body, such as VerificationUrl with exact values.
Line 7 onward: We are sending Mail using Google SMTP Servers, so rest is the Same Process.
Replace Email ID and Password with your Google ID/Password to send Mail.
Confirm.aspx
Put a Additional Textbox, if you want to Put Code also. But in above case, we are just using Confirmation Link, so place a Link in this Page to set the Status of Success.
Now What we will do is, First Find GUID from QueryString (passed in the ConfirmationLink.html page and send to Email ID) and activate the user.
Code for backend in Confirm.aspx
Lets begin with the Registration Feature of ASP.NET membership that will send Email with confirmation code or Activation link to the newly registered email id.
When user will click link, then account will be active according to the code or link we have send to the email id.
Lets begin with the assets we have:
Visual Studio 2012 Ultimate
Windows 8 Pro
MS Sql Server 2008 R2
C# for Backend Language
I am here assuming that you have the basic knowledge of ASP.NET.
Step 1: Creating a Website
Open Visual Studio and Click on New >> Website and Choose Empty Website in your Required Framework version in Required Language (vb or c#).
Step 2: Add New Page and Control
Right Click on Solution Name and Click Add Item and Choose Web Form.
We need at least below Pages and Items in Solution.
Home Page (Default.aspx)
Login Page (Login.aspx)
Registration (Register.aspx)
Confirm Page (Confirm.aspx)
Link for Mail (ConfirmationCode.html)
Additional forms and Folders as per requirement.
What we will do in above forms.
In Default.aspx we will put a Login View (Login/Logout) LoginName control.
In Login.aspx we are putting a Login Control in this Page.
In Register.aspx we will put a CreateUserWizard control to register.
In Confirm.aspx we will put some code in Backend and accept the Confirmation Code based in URL, or Entered Code.
In ConfirmationCode.html we will put Generated Code for Sending Mail. This will be attachment of Mail Body.
Open your Toolbox of Visual Studio and Insert a CreateUserWizard from Login Section in Form according to your Design as mentioned above and other designs.
Step 3:Customization and Accessories
web.config
We need to add some note in web.config file. Here is the Code that is required in web.config.
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\sqldb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms loginUrl ="Login.aspx" timeout="200"></forms>
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
</system.web>
<location path="Default.aspx">
<system.web>
<authorization>
<deny users ="?"/>
</authorization>
</system.web>
</location>
<location path="Register.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
</configuration>
Replace your Instance ID in above ID for Connection String.ConfirmationCode.html
Lets Type the Body of HTML Message that will be send on Registration.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Confirm Your Registration</title>
</head>
<body>
Hi <b><%Username%></b>,<br />
We are feeling Glad to welcome you in our Community. <br />
You are just one step far from your friends. Please click below link to confirm your Registration and get in touch.<br />
<br />
<b><%VerificationUrl%></b>
<br /><br />
Address,<br />
Address
</body>
</html>
We have put two Variables Usernames and VerificationUrl in Message and will replace these keywords before sending mail.Register.aspx
Open the Properties of Registration Control and by Pressing F4 after selecting Control or Right Click and Choosing Property.
Set DisableCreateUser option to True
Also set MailDefinition for sending mails and Define attachment file.
<MailDefinition From="support@mysite.com" Subject="Confirmation Link" IsBodyHtml="true" BodyFileName="~/ConfirmationCode.html">
</MailDefinition>
Now Right Click on CreateUserWizard Control and Choose Event after selecting Property window.
From the Events write this code on SendingMail Event.
Lets see the Code for Sending Mail.
Namespaces Required:
System.Net for Sending Mail and System.Web.Security to access Membership features.
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 = "/Confirm.aspx?memberID=" + 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("support@mysite.com", "xxxxxxxxxx");
smtp.EnableSsl = true;
smtp.Send(e.Message);
e.Cancel = true;
}
Description of Above Code:
Line 1: We get the Username of Currently Created User.
Line 2: We get UserKey of currently Created User and converted it to GUID Format.
Line 3: We find name of Domain where it is being Hosted. Only Domain (Left Part before / and Application path if any).
Line 4: We put path to Confirm page on a String variable to join with Domain.
Line 5: We joined Confirmation Link and Domain Name to form a Complete URL.
Line 6: We Replaced Keywords of Mail Body, such as VerificationUrl with exact values.
Line 7 onward: We are sending Mail using Google SMTP Servers, so rest is the Same Process.
Replace Email ID and Password with your Google ID/Password to send Mail.
Confirm.aspx
Put a Additional Textbox, if you want to Put Code also. But in above case, we are just using Confirmation Link, so place a Link in this Page to set the Status of Success.
Now What we will do is, First Find GUID from QueryString (passed in the ConfirmationLink.html page and send to Email ID) and activate the user.
Code for backend in Confirm.aspx
Guid newUserId = new Guid(Request.QueryString["memberID"]);
MembershipUser newUser = Membership.GetUser(newUserId);
if (newUser == null)
{
Label1.Text = "User Account not found";
}
else
{
newUser.IsApproved = true;
Membership.UpdateUser(newUser);
Label1.Text = "Account Approved, please click <a href='~/Login.aspx' target='_self'>login</a> to continue";
}
Now your Account is active and you can login to your account.