Model
using System.Data.Entity;
using
System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
namespace MyProject.Models
{
[Table("AdminMaster")]
public class AdminMaster
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ADMId { get; set; }
[Display(Name = "Admin Name")]
public string ADMName { get; set; }
[Display(Name = "Username")]
public string ADMUsername { get; set; }
[Display(Name = "Email")]
public string ADMEmail { get; set; }
[Display(Name = "Password")]
public string ADMPassword { get; set; }
[Display(Name = "Type")]
public string ADMType { get; set; }
}
public class AdminMasterContext : DbContext
{
public AdminMasterContext() : base("ConString_Name")
{
}
public DbSet<AdminMaster>
AdminMaster { get; set; }
}
}
Controller
using System.Linq;
using System.Web.Mvc;
using MyProject.Models;
namespace MyProject.Areas.Admin.Controllers
{
public class AdminController : Controller
{
AdminMasterContext
_admin = new AdminMasterContext();
// GET: Admin/Admin
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(AdminMaster
adminMaster)
{
var admin =
_admin.AdminMaster.Where(b=> b.ADMUsername.Equals(adminMaster.ADMUsername)
&& b.ADMPassword.Equals(adminMaster.ADMPassword)).FirstOrDefault();
if (admin != null)
{
Session["AdminEmail"] =
admin.ADMEmail;
Session["AdminId"] =
admin.ADMId;
Session["Username"] =
admin.ADMId;
Session["AdminType"] =
admin.ADMType;
return RedirectToAction("Index", "Dashboard", new { area = "Admin" });
}
else
{
ViewBag.ErrorMsg = "Invalid username or
password";
}
return View();
}
public ActionResult Logout()
{
Session.Abandon();
Session.RemoveAll();
return RedirectToAction("Login", "Admin", new { area = "Admin" });
}
}
}
View
@model MyProject.Models.AdminMaster
@{
ViewBag.Title = "Login";
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Login - CSIT Cable</title>
<link type="image/png" href="~/Content/images/icon.png" rel="icon" />
<link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/css/cable.css" rel="stylesheet" />
<link href="~/Content/css/font-awesome.min.css" rel="stylesheet" />
<link href="~/Content/css/icofont.css" rel="stylesheet" />
<link href="~/Content/css/animate.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
</head>
<body>
<div class="lg-main">
<div class="container lg-main-1">
<div class="row">
<div class="col-md-6 col-sm-6
lg-main-left" style="background-color: #f5f5f5;">
<center>
<img src="~/Content/images/logo4.gif" />
</center>
</div>
<div class="col-md-6 col-sm-6
lg-main-right">
<center>
<h3>login</h3>
<div class="lg-main-i">
<i class="icofont
icofont-users-alt-3"></i>
</div>
@using (Html.BeginForm())
{
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="email" type="text" class="form-control" name="ADMUsername" placeholder="Enter username">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icofont icofont-lock"></i></span>
<input id="password" type="password" class="form-control" name="ADMPassword" placeholder="Enter password">
</div>
<p class="login-error" style="color:red; font-family:Bahnschrift">@ViewBag.ErrorMsg</p>
<input type="submit" name="" value="login" class="lg-main-sub">
}
</center>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="~/Content/js/bootstrap.min.js"></script>
<script src="~/Content/js/jquery.min.js"></script>
</body>
</html>