Create a Simple Login Page in MVC

Step 1: Create a view as follow :

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCTEST1.Models.LogOnModel>" %>

 

<!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 id="Head1" runat="server">

    <title>Login</title>

</head>

<body>

    <div>

        <h2>

            Login</h2>

        <%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>

        <% using (Html.BeginForm())

           {%>

        <fieldset>

            <legend>Fields</legend>

            <p>

                <label for="Name">

                    Name:</label>

                <%= Html.TextBox("UserName")%>

                <%= Html.ValidationMessage("Id", "*") %>

            </p>

            <p>

                <label for="Passward">

                    Passward:</label>

                <%=Html.Password("Password") %>

                <%= Html.ValidationMessage("Passward", "*") %>

            </p>

            <p>

                <label for="Email">

                    Email:</label>

                <%= Html.TextBox("Email") %>

                <%= Html.ValidationMessage("Email", "enter it") %>

            </p>

            <p>

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

            </p>

        </fieldset>

        <% } %>

    </div>

    <div>

        <%=Html.CheckBox("chk1", Convert.ToBoolean(ViewData["status"].ToString()))%>

        <%= Html.Label(ViewData["Login"].ToString())%>

    </div>

</body>

</html>

Step 2: Create a model as follows :

#region Login Model

public class LogOnModel

{

    [DisplayName("id")]

    public int id { get; set; }

    [Required]

    [DisplayName("User name")]

    public string UserName { get; set; }

    [Required]

    [DataType(DataType.Password)]

    [DisplayName("Password")]

    public string Password { get; set; }

    [Required]

    [DisplayName("Email")]

    public string Email { get; set; }

}

#endregion

#region Interface for Test Module

public interface IMembershipService

{

    DataTable check_login(string userid, string password);

    string register(string username, string password, string F_Name, string L_Name, string Address, string Mobile, string City);

}

#endregion

#region Methods that used in controller

public class login_methods : IMembershipService

{

    SqlConnection conn = new SqlConnection(@"server=F10\SQL;Initial Catalog=pankaj;User Id=sa;Password=abc@123");

    public DataTable check_login(string userid, string password)

    {

 

        SqlCommand cmd = new SqlCommand("select * from User_Login where Username=@user and User_Pass=@pass", conn);

        cmd.Parameters.AddWithValue("@user", userid);

        cmd.Parameters.AddWithValue("@pass", password);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();

        da.Fill(dt);

 

        return dt;

    }

 

}

#endregion

Step 3: Create a controller.

static List<LogOnModel> sun = new List<LogOnModel>();

public IMembershipService service { get;set;}

#region Initialization

protected override void Initialize(RequestContext requestContext)

{

if (service == null) { service = new login_methods();

}

base.Initialize(requestContext);

}

#endregion

#region For Login Page

public ActionResult Login()

{

ViewData["Login"] = "";

ViewData["status"] = false;

return View();

}

// [AcceptVerbs(HttpVerbs.Post)]

[HttpPost]

public ActionResult Login(LogOnModel pkp)

{

ViewData["Login"] = "";

ViewData["status"] = false;

if (!ModelState.IsValid)

{

return View("Login", pkp);

}

else

{

DataTable data=new DataTable();

data=service.check_login(pkp.UserName.ToString(),pkp.Password.ToString());

 

if (data.Rows.Count>0)

{

//Session["user"]=data;

sun.Add(pkp);

return RedirectToAction("Default");

}

else

{

ViewData["Login"] = "Login Failed!!";

ViewData["status"] = true;

return View("Login", pkp);

}

}

}

#endregion

Step 4: Run and Test

Ebook Download
View all
Learn
View all