Verify Access

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

<!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>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p align="left">
             <asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" ShowSummary="false" ValidationGroup="fldValGrp1" runat="server" />
        </p>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserName" SetFocusOnError="true" ErrorMessage="Please a valid email address."> * </asp:RequiredFieldValidator>
        <asp:Label ID="label1" Text="Username:" runat="server"></asp:Label>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPassword" SetFocusOnError="true" ErrorMessage="Please a valid password."> * </asp:RequiredFieldValidator>
        <asp:Label ID="label2" Text="Password:" runat="server"></asp:Label>
        <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" ValidationGroup="fldValGrp1" runat="server" />
    </div>
    </form>
</body>
</html>
 
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetstairAccessVerifyer;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //- Must be a valid email address
        if (this.txtUserName.Text.Trim().Length > 0 && this.txtPassword.Text.Trim().Length > 0)
        {
            int iUserId = 101; //- Assuming the user Id = 101
            Session["userId"] = iUserId.ToString();
            Session["user_name"] = this.txtUserName.Text.Trim();

            if (VerifyAccess.IsTheDeviceAccessVerified(iUserId, Request.UserHostName))
            {
                //- Let's go home.
                Response.Redirect("~/Welcome.aspx");
            }
            else
            {
               VerifyAccess.GenerateMyAccessCode(iUserId, txtUserName.Text.Trim());
               if (VerifyAccess.RecordAffected > 0)
               {
                   //- Send the Code via email to the valid user trying to login
                   var TheVerificationCode = Session["myapp_accesscode"].ToString();
                   string BODY_VERIFICATION_CODE_CONTENT = "<p style='font-size: 14pt; font-weight: bold' align='left'>Your Verification code: " + TheVerificationCode + "</p>";
                   BODY_VERIFICATION_CODE_CONTENT += "<p style='font-size: 10pt;' align='left'><a href='https://www.yoursite.com/Verify.aspx'>https://www.yoursite.com/Verify.aspx</a></p>";

                   //- Send the Verification Code to the user on file via email here
                   Response.Write(BODY_VERIFICATION_CODE_CONTENT);
                   //- Verify Code
                   Response.Redirect("Verify.aspx");
                }
              }
           }        
    }
}
 
 
Verify.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Verify.aspx.cs" Inherits="Verify" %>

<!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>
         <p align="left">
             <asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" ShowSummary="false" ValidationGroup="fldValGrp1" runat="server" />
         </p>
         <p align="center">It appears that your device has not been verified.</p>
         <p align="center">
             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtAccessCodeToVerify" SetFocusOnError="true" ErrorMessage="Please enter the valid access code sent to your email."> * </asp:RequiredFieldValidator>Code:
             <asp:TextBox ID="txtAccessCodeToVerify" runat="server"></asp:TextBox>
             <asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" ValidationGroup="fldValGrp1" runat="server" />
             <asp:Button ID="btnReSend" Text="Resend Code" OnClick="btnResendCode_Click" runat="server" />
         </p>         
    </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;
using NetstairAccessVerifyer;

public partial class Verify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.txtAccessCodeToVerify.Text.Trim().Length > 0)
        {   
            //---if (Session["myapp_accesscode"].ToString() == this.txtAccessCodeToVerify.Text.Trim())
            if (VerifyAccess.IsValidVerificationAccessCode(this.txtAccessCodeToVerify.Text.Trim()))
            {
                Response.Redirect("~/Welcome.aspx");
            }
            else
            {
                Response.Write("<script>alert('Please enter the valid code, sent to your email.');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Please enter the valid code, sent to your email.');</script>");
        }        
    }
    protected void btnResendCode_Click(object sender, EventArgs e)
    {
        int iUserId = Int32.Parse(Session["userId"].ToString());
        string sLoginUserName = Session["user_name"].ToString();
        if (iUserId > 0 && sLoginUserName.Trim().Length>0)
        {
            VerifyAccess.GenerateMyAccessCode(iUserId, sLoginUserName);
            if (VerifyAccess.RecordAffected > 0)
            {
                //- Send the Code via email to the valid user trying to login
                //- Send the Code via email to the valid user trying to login
                var TheVerificationCode = Session["myapp_accesscode"].ToString();
                string BODY_VERIFICATION_CODE_CONTENT = "<p style='font-size: 14pt; font-weight: bold' align='left'>Your Verification code: " + TheVerificationCode + "</p>";
                BODY_VERIFICATION_CODE_CONTENT += "<p style='font-size: 10pt;' align='left'><a href='https://www.yoursite.com/Verify.aspx'>https://www.yoursite.com/Verify.aspx</a></p>";
                //- Send it via email here {sLoginUserName} Stored in Session

            }
        }
    }
}
Web.Config
<?xml version="1.0"?>
<configuration>
  <appSettings>    
    <!-- MS-Access Default Connection String --> 
    <add key="netstair-database" value="D://Netstaircom//Netstair-Verifier-Sample//NVS-Sample//App_Data//NetstairDb.mdb"/>
    <!-- SQL Database Connection String / Uncomment below if you are using SQL Database -->
    <!-- add key="netstair-database" value="Provider=SQLOLEDB;Data Source=(local);Initial Catalog={Database name here};User Id={User Id Here};Password={Password Here};Pooling = true; Max Pool Size = 20;Min Pool Size = 5; connection timeout=180;MultipleActiveResultSets=True"/ -->
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>
 
Download Sample here --> Sample Code

Add Feedback