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 ASPSnippet.com. You can any others to do the SMS functionality.
First create a database named DBTest and create a table named tblUser.
Now create a new project...
add a login page with two panel. One for login and one for mobile verification.
In the Code behind part write down the code.
Now add the home page. And your project is ready. To get the SMS API go here in http://www.aspsnippets.com/.
http://www.aspsnippets.com/Articles/How-to-send-free-SMS-from-ASPNet-application-to-Mobile.aspx
You can download the full source code here.
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.
<body> <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> <asp:Panel ID="PanelMobile" runat="server" Visible="false" style="padding: 10px 10px 10px 10px" BorderColor="#0099FF" BorderStyle="Solid" BorderWidth="1px"> <h1>Code</h1> <br /> <br /> <asp:TextBox ID="txtCode" runat="server" placeholder="Code"></asp:TextBox> <br /> <asp:Button ID="btnOk" runat="server" Text="Ok" onclick="btnOk_Click" /> </asp:Panel> </div> </form> </body>
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();
da.Fill(dt);
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
}
else
{
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"];
Session.Remove("TempName");
Session.Remove("Code");
Response.Redirect("home.aspx");
}
else
{
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 http://www.aspsnippets.com/.
http://www.aspsnippets.com/Articles/How-to-send-free-SMS-from-ASPNet-application-to-Mobile.aspx
You can download the full source code here.