This example shows you how to create login page and validating user details in MVC using entity frame work.
Output :
Implementation:You can download the sample project from here....
Download- Create Database and name it as "sample"
- Run the below Query to create table "tblUser"
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblUser](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED
(
[UserID] 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
- Add sample user details

- In the project solution under Model add LoginModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MVCEntityLogin.Models
{
public class LoginModel
{
[Required(ErrorMessage = "Please enter user name.")]
[DataType(DataType.EmailAddress)]
[Display(Name = "User Name")]
[StringLength(30)]
public string UserName { get; set; }
[Required(ErrorMessage = "Please enter password.")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[StringLength(10)]
public string Password { get; set; }
}
[Table("tblUser")]
public class tblUser
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}
- Under Controllers add HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCEntityLogin.Models;
namespace MVCEntityLogin.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel login)
{
if (ModelState.IsValid)
{
DBEntity db = new DBEntity();
var user = (from userlist in db.tblUsers
where userlist.UserName == login.UserName && userlist.Password == login.Password
select new
{
userlist.UserID,
userlist.UserName
}).ToList();
if (user.FirstOrDefault() != null)
{
Session["UserName"] = user.FirstOrDefault().UserName;
Session["UserID"]=user.FirstOrDefault().UserID;
return Redirect("/home/welcomepage");
}
else
{
ModelState.AddModelError("", "Invalid login credentials.");
}
}
return View(login);
}
public ActionResult WelcomePage()
{
return View();
}
}
}
- Now add the Views under Views->Home
Login.cshtml@model MVCEntityLogin.Models.LoginModel
@{
ViewBag.Title = "Login";
}
<h2>
Login</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div style="border: solid 1px #ccc; padding: 30px 0 30px 30px; border-radius: 5px;
width: 325px; margin: auto; display: table;">
<table>
<tr>
<td valign="top">
@Html.LabelFor(model => model.UserName)
</td>
<td>
@Html.EditorFor(model => model.UserName)
<div style="height: 20px;">@Html.ValidationMessageFor(model => model.UserName)
</div>
</td>
</tr>
<tr>
<td valign="top">
@Html.LabelFor(model => model.Password)
</td>
<td>
@Html.EditorFor(model => model.Password)
<div style="height: 20px;">@Html.ValidationMessageFor(model => model.Password)
</div>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Login" />
</td>
</tr>
</table>
</div>
}
WelcomePage.cshtml@{
ViewBag.Title = "WelcomePage";
}
<h2>
Welcome : @Session["UserName"].ToString()</h2>
You can download the sample project from here....
Download