Friday, June 20, 2014

2-Step Verification like Google in ASP.NET C#

In this article I have done a log in function with 2-Step verification as like we see in Google or in Facebook. To send a message I have used Site2SMS API from You can any others to do the SMS functionality.

First create a database named DBTest and create a table named tblUser.

CREATE TABLE [dbo].[tblUser](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Email] [nvarchar](50) NULL,
 [Name] [nvarchar](50) NULL,
 [Password] [nvarchar](50) NULL,
 [PhoneNo] [nvarchar] (20)

Now create a new project...

add a login page with two panel. One for login and one for mobile verification.

    <form id="form1" runat="server">
    <div align="center">
        <asp:Panel ID="PanelLogin" runat="server" style="padding: 10px 10px 10px 10px" 
            BorderColor="#0099FF" BorderStyle="Solid" BorderWidth="1px">
            <h1>Login Now</h1>
            <asp:TextBox ID="txtEmail" runat="server" placeholder="Email"></asp:TextBox>
            <br />
            <asp:TextBox ID="txtPassword" runat="server" TextMode ="Password" placeholder="Password"></asp:TextBox>
            <br />
            <asp:Button ID="btnLogin" runat="server" Text="Login" 
                onclick="btnLogin_Click" />
        <asp:Panel ID="PanelMobile" runat="server" Visible="false"  style="padding: 10px 10px 10px 10px" 
            BorderColor="#0099FF" BorderStyle="Solid" BorderWidth="1px">
            <br />
            <br />
            <asp:TextBox ID="txtCode" runat="server" placeholder="Code"></asp:TextBox>
            <br />
            <asp:Button ID="btnOk" runat="server" Text="Ok" onclick="btnOk_Click" />

In the Code behind part write down the code.

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DBCon"].ToString());
protected void btnLogin_Click(object sender, EventArgs e)
     string sql = "select * from tblUser where email='" + txtEmail.Text.Trim() + "' and  password='" + txtPassword.Text.Trim() + "' ";
     SqlDataAdapter da = new SqlDataAdapter(sql,con);
     DataTable dt = new DataTable();
     if (dt.Rows.Count > 0)
          PanelLogin.Visible = false;
          PanelMobile.Visible = true;
          Session["Code"] = generate_code();
          Session["TempName"] = dt.Rows[0]["Name"].ToString();
          sendSMS(Session["Code"].ToString(), dt.Rows[0]["PhoneNo"].ToString();
          //txtCode.Text = Session["Code"].ToString();       only for testing purpose
          Response.Write("<script>alert('Wrong username and password');</script>");
private string generate_code()
      var chars = "0123456789";
      var stringChars = new char[5];
      var random = new Random();
      for (int i = 0; i < stringChars.Length; i++)
          stringChars[i] = chars[random.Next(chars.Length)];
      var finalString = new String(stringChars);
      return finalString.ToString();
protected void btnOk_Click(object sender, EventArgs e)
      if (txtCode.Text.Trim()==Session["Code"].ToString())
            Session["Name"] = Session["TempName"];
            Response.Write("<script>alert('Wrong code');</script>");
private void sendSMS(string code, string phone)
       SMS.APIType = SMSGateway.Site2SMS;
       SMS.MashapeKey = "<Your API Key>";
       SMS.Username = "<Login Id>";
       SMS.Password = "<Password>";
       SMS.SendSms(phone, "Your code is " + code + ".");

Now add the home page. And your project is ready. To get the SMS API go here in

You can download the full source code here.


Post a Comment

Popular Posts