In this blog, you can have the complete source code of Login Functionality With SQL Server Database, Sessions With Log Out Functionality In ASP.Net MVC.
HomeController Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LoginFormMvc.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
if(Session["username"] == null)
{
return RedirectToAction("Index","Login");
}
return View();
}
}
}
LoginController Source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LoginFormMvc.Models;
namespace LoginFormMvc.Controllers
{
public class LoginController : Controller
{
LoginDBEntities db = new LoginDBEntities();
// GET: Login
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(user s)
{
if(ModelState.IsValid == true)
{
var credentials = db.users.Where(model => model.username == s.username && model.password == s.password).FirstOrDefault();
if(credentials == null)
{
ViewBag.ErrorMessage = "Login Failed";
return View();
}
else
{
Session["username"] = s.username;
return RedirectToAction("Index","Home");
}
}
return View();
}
public ActionResult Logout()
{
Session.Abandon();
return RedirectToAction("Index","Login");
}
public ActionResult Reset()
{
ModelState.Clear();
return RedirectToAction("Index", "Login");
}
}
}
DbContext Class Source code – LoginDbModel.context.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace LoginFormMvc.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class LoginDBEntities : DbContext
{
public LoginDBEntities()
: base("name=LoginDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<user> users { get; set; }
}
}
User.cs Model Class Source Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace LoginFormMvc.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class user
{
public int id { get; set; }
[DisplayName("Username")]
[Required(ErrorMessage = "Username is required")]
public string username { get; set; }
[DisplayName("Password")]
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string password { get; set; }
}
}
Index View Of HomeController – Source Code
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h3>Welcome @Session["username"]</h3>
@Html.ActionLink("LogOut","Logout","Login")
Index View Of LoginController – Source Code
@model LoginFormMvc.Models.user
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div class="row">
<div class="col-md-3 col-md-offset-4">
<div style="border:5px black ridge; padding:10px;">
@using (Html.BeginForm("Index","Login",FormMethod.Post))
{
<table height="200">
<tr>
<th colspan="2" class="text-center" style="font-size:25px;">LOGIN</th>
</tr>
<tr>
<td>@Html.LabelFor(model => model.username)</td>
<td>
@Html.TextBoxFor(model => model.username,new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.username,"", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.password)</td>
<td>
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
<p class="text-danger">@ViewBag.ErrorMessage</p>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Login" name="login" class="btn btn-info" />
<a href="/Login/Reset" class="btn btn-danger">Reset</a>
</td>
</tr>
</table>
}
</div>
</div>
</div>
_Layout.cshtml – Master Page View Source Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</body>
</html>
Click Below Link to Download Notes & Source Code Of This Blog
https://www.mediafire.com/file/0cgx2tdvafh5rx9/LoginFormMvc.rar/file
No responses yet