Monday 21 August 2023

CaptchaMvc Mvc5 | C# ASP.Net

 CaptchaMvc Mvc5 Using C# ASP.Net Visual Studio 2019



CAPTCHA stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". It is a test used to distinguish between humans and computers. CAPTCHAs are often used to prevent automated spam and abuse.


CaptchaMvc will implement your web MVC applications easier and more reliable protection.

Features:

You can easily change or extend the current implementation of the captcha.

By default there are two types of captcha, plain and mathematical.

Supports MVC 3, MVC 4, MVC 5.

Supports for storing captcha in the session or cookie.

Supports the "intelligent" captcha.

Lets Start follow by steps given below

Step 1 (Download Package)

Download CaptchaMvc.Mvc5 . Goto Tools > Nuget Package Manager > Manage Nuget Packages for Solution 

Goto Browse table and search CaptchaMvc


Or Goto https://www.nuget.org/packages/CaptchaMvc.Mvc5/1.5.0?_src=template#readme-body-tab for information about  CaptchaMvc.MV5 Installation

Step 2 ( Integrate in Razor - HTML)

@using CaptchaMvc.HtmlHelpers

@using CaptchaMvc;

<form method="post">

    @Html.AntiForgeryToken()

    <input type="text" name="UserName" id="UserName" required /><br />

    <input type="password" name="Password" id="Password" required /><br />

    @Html.Captcha("Refresh", "Enter Text you see in Image", 3)

    <span style="color:red">@ViewBag.ErrMessage </span>

    <input type="submit" value="Login" name="submit" />

</form>

Step 3 (Model)

public class UserModel
{

    public string ID { get; set; }

    public string Name { get; set; }

    public string UserName { get; set; }

    public string Password { get; set; }
}

Step 4 (Controller)

using CaptchaMvc.HtmlHelpers;
using ayms.Models;     // Place your project model name here

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(UserModel data)
{
    // Code for validating the CAPTCHA 

    if (this.IsCaptchaValid("Captcha is not valid "))
    {
        SelectRepository ob = new SelectRepository();
        int AdminID = 0;   
        string ErrMsg = string.Empty;     
        if (ModelState.IsValid)
        {

            try
            {
                if (ob.LoginAdmin(data, ref AdminID, ref ErrMsg))
                {
                    int timeout = 525600;
                    FormsAuthentication.SetAuthCookie(data.UserName, true);
                    return RedirectToAction("Index", "admin");
                }
                else
                {
                    if (ErrMsg == "")
                        return RedirectToAction("msg", "common", new { msg = "Invalid User Name or Password" });
                    else
                        return RedirectToAction("msg", "common", new { msg = ErrMsg });
                }

            }
            catch (Exception ex)
            {
               return RedirectToAction("msg", "common", new { msg = ex.Message  });
            }
        }
        else
            return RedirectToAction("msg", "common", new { msg = "Invalid Captcha or bots Error" });

 // for show error msg view is made in common control. You implement your won logic to show error.

    }
    ViewBag.ErrMessage = "Error: captcha is not valid.";
    return View();

}

Step 5 (Repository database code)


public bool LoginAdmin2(UserModel data,  ref string ErrMsg)
{
    string result = "";
    try
    {
        using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("[dbo].[LoginAdmin]", dbcon))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@UserName", data.UserName);                                        cmd.Parameters.AddWithValue("@Password", data.Password);
                if (dbcon.State == ConnectionState.Closed)
                    dbcon.Open();
                if (cmd.ExecuteScalar().ToString() == "1")
                {                          
                    result = "1";
                }
                else
                    result = "0";
            }
        }
    }
    catch (Exception ex)
    {
        ErrMsg = ex.Message;
    }
    if (result == "1")
        return true;
    else
        return false;

}

Step 6 (Table Structure)



Step 7  (Store Procedure)


USE [girfa_ayms]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- admin login
create PROCEDURE [dbo].[LoginAdmin]
 (  
       @UserName varchar(50),
    @Password NVARCHAR(100) 
  )
  as
BEGIN
SET NOCOUNT ON
declare @role int
if (select count(*) from AdminMaster where UserName=@UserName and Pass=@Password and Status=1)=1
begin 
       select '1'
end
else
       select '0'
END 

No comments:

Post a Comment