How to send confirmation after registration using Asp.Net and C#


In order to validate the email address of the user provided during registration time, a confirmation email with activation link is sent to the email address and when user clicks the activation link, his email address is verified and his account gets activated.

First create database table.

Create Database :



CREATE TABLE [dbo].[Reg_Form]( // Database Table Name
[uname] [nvarchar](50) NULL, // Username
[pwd] [nvarchar](50) NULL, // Password
[email] [nvarchar](50) NULL, // Email Address
[act_code] [uniqueidentifier] NULL, // Activation Code or Confirmation Code
[acc_status] [nvarchar](15) NULL // Account Status
)

Create ASP.NET Page :



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
<div>
<table >
<tr>
<th colspan="2">Registration</th>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td style="width:200px; text-align:center;">Username :</td>
<td><asp:TextBox ID="txtusername" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtusername" >enter username</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width:200px; text-align:center;">Password :</td>
<td><asp:TextBox ID="txtpassword" runat="server" CssClass="txtbox" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtpassword">enter password</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width:200px; text-align:center;">Email ID :</td>
<td><asp:TextBox ID="txtemailid" runat="server" CssClass="txtbox"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtemailid"
ValidationExpression="\w+undefined[-+.']\w+)*@\w+undefined[-.]\w+)*\.\w+undefined[-.]\w+)*">please enter valid email addressundefinedabc@xyz.com)</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Btn_Register" runat="server" Text="Register"
onclick="Btn_Register_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


C# Coding for Registration Page :





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Net.Mail;

public partial class Registration : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RCCon"].ConnectionString);
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{

}
// Send Email to user email address
public void Send_Account_Activation_Link(string emailaddress, string act_code)
{
MailMessage mm = new MailMessage("Your EMail ID", emailaddress);
mm.Subject = "Account Activation";
string body = "Hello " + txtusername.Text + ",";
body += "

Please click the following link to activate your account
";
body += "
< a style="background:#000000; color:#fafafa; padding:10px 100px 10px 100px; width:350px; text-decoration:none; font-weight:bold; font-size:20px;" href=""+ Request.Url.AbsoluteUri.Replace("Registration.aspx", "Account_Activation.aspx?ActivationCode=" + act_code) + "">Click here to activate your account.< /a >";
body += "

Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("Your Email ID", "");
smtp.EnableSsl = true;
smtp.Send(mm);
}

protected void Btn_Register_Click(object sender, EventArgs e)
{
string activationCode = Guid.NewGuid().ToString();
string encry_password = Encrypt_Password(txtpassword.Text);
cmd = new SqlCommand("insert into Registration values('" + txtusername.Text.ToLower() + "','" + encry_password + "','" + txtemailid.Text + "','" + activationCode + "','inactive')");
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
Send_Account_Activation_Link(txtemailid.Text, activationCode);
con.Close();
Session["user"] = txtusername.Text;
Response.Redirect("Account_Activation.aspx");
}

private string Encrypt_Password(string password)
{
string pwdstring = string.Empty;
byte[] pwd_encode = new byte[password.Length];
pwd_encode = Encoding.UTF8.GetBytes(password);
pwdstring = Convert.ToBase64String(pwd_encode);
return pwdstring;
}

}

---------------------------------------------------------------------------------------------------------------

Create Login Page :



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<th colspan="2" >Login Page</th>
</tr>
<tr >
<td>Username :</td>
<td><asp:TextBox ID="txtusername" runat='server' CssClass="txtbox"></asp:TextBox></td>
</tr>
<tr>
<td >Password :</td>
<td><asp:TextBox id="txtpassword" runat="server" CssClass="txtbox" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Btn_Login" runat="server" onclick="Btn_Login_Click" Text="Login" />
</td>
</tr>
<tr>
<td colspan="2" align="left">
<label id="lblerror" runat="server"></label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


C# Coding for Login Page :



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.IO;

public partial class login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RCCon"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Btn_Login_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Registration", con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string userid = ds.Tables[0].Rows[i]["username"].ToString();
string pwd = Decrypt_Password(ds.Tables[0].Rows[i]["password"].ToString());
string status = "active";
if (status == ds.Tables[0].Rows[i]["account_status"].ToString())
{

if (userid == txtusername.Text.ToLower() && pwd == txtpassword.Text)
{
Response.Redirect("Default.aspx?Username=" + txtusername.Text);
}
}

lblerror.InnerText = "Invalid Username and Password";

}
}
}
private string Decrypt_Password(string encryptpassword)
{
string pwdstring = string.Empty;
UTF8Encoding encode_pwd = new UTF8Encoding();
Decoder Decode = encode_pwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpassword);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
pwdstring = new String(decoded_char);
return pwdstring;
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: