OTP - One-time password

ASP.NET  one-time password

Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="NetstairOTPTest.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>OTP Testing</title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>

    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    
</head>
<body>
    <form id="form1" runat="server">
        <div class="panel panel-default">
             <div class="panel-body">Login</div>
                   <div class="form-group">
                        <label for="InputEmail">Username</label>                        
                         <asp:TextBox ID="txtUsername" CssClass="form-control" aria-describedby="emailHelp" placeholder="Enter email" runat="server" required></asp:TextBox>
                    </div>
                    <div class="form-group">
                        <label for="InputPassword">Password</label>                        
                        <asp:TextBox ID="txtPassword" CssClass="form-control" TextMode="Password" runat="server" placeholder="Password" required></asp:TextBox>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="exampleCheck1">
                        <label class="form-check-label" for="exampleCheck1">Remember Me</label>
                    </div>
                    <p>&nbsp;</p>
                                        <asp:Button ID="btnSubmit" CssClass="btn btn-primary" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
        </div>
    </form>
</body>
</html>
Login.aspx.c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NetstairOTPTest
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {   
                Session["authenticateuser"] = txtUsername.Text;
                if (NetstairOTP.VerifyAccess.IsTheDeviceAccessVerified(txtUsername.Text, Request.UserHostAddress.ToString(), Request.Browser.Browser.ToString(), Session.SessionID.ToString()))
                {
                    Response.Redirect("~/");  //- Go Home
                }
                else
                {
                    Response.Redirect("~/Verify.aspx");  //- Verify Authentication
                }
            }
        }
    }
}
Verify.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Verify.aspx.cs" Inherits="NetstairOTPTest.Verify" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OTP testing</title>
</head>
<body>
    <form id="form1" runat="server">
        <div class="panel panel-default">
             <div class="panel-body">One-time password Verification</div>
                   <div class="form-group">
                        <label for="InputCode">OTP Code</label>                       
                         <asp:TextBox ID="txtOTPCode" runat="server" placeholder="OTP Code" required></asp:TextBox>
                    </div>                    
                    <p>&nbsp;</p>                    
                    <asp:Button ID="btnSubmit" CssClass="btn btn-primary" Text="Submit" OnClick="btnSubmit_Click" runat="server" />
        </div>
    </form>
</body>
</html>
Verify.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NetstairOTPTest
{
    public partial class Verify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               
            }
        }      
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string Authenticated_Username = "";
                if (Session["authenticateuser"] != null)
                {
                    Authenticated_Username = Session["authenticateuser"].ToString();
                }
                var OTPCode_Value = Request.Form["txtotpcode"].ToString();
                if (NetstairOTP.VerifyAccess.IsValidVerificationOTPCode(OTPCode_Value, Authenticated_Username, Request.Browser.Browser.ToString(), Session.SessionID))
                {
                    Response.Redirect("~/login.aspx");
                }
                else
                {
                    Response.Write("Invalid Code");
                }
            }
        }
    }
}
You will need this table to be created in your database
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Access_Log](
	[logId] [int] IDENTITY(1,1) NOT NULL,
	[log_date] [datetime] NULL,
	[user_name] [varchar](255) NULL,
	[browser_type] [varchar](255) NULL,
	[remoteIp] [varchar](50) NULL,
	[access_code] [varchar](50) NULL,
	[platform_os] [varchar](120) NULL,
	[location] [varchar](255) NULL,
	[referenceId] [varchar](255) NULL,
 CONSTRAINT [PK_Access_Log] PRIMARY KEY CLUSTERED 
(
	[logId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
NetstairOTP.dll needs to be referenced to your project. [Click here to download]--> NetstairOTP.dll

Download whole test project here [Click on NetstairOTPTest.zip to download]

Add Feedback