Login Control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LoginForm.Models;
namespace LoginForm.Controllers
{
public class LoginController : Controller
{
// GET:
Login
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(User u)
{
// this
action is for handle post (login)
if (u != null) // this is check validity
{
using (NareshEntities dc = new NareshEntities())
{
var v = dc.Users.Where(a =>
a.UserName.Equals(u.UserName) &&
a.PassWord.Equals(u.PassWord)).FirstOrDefault();
if (v != null)
{
Session["LogedUserID"] =
v.UserId.ToString();
Session["LogedUserFullname"] = v.FullName.ToString();
return RedirectToAction("AfterLogin");
}
else
{
ViewBag.Message="Invalid UserName and Password";
}
}
}
return View(u);
}
public ActionResult AfterLogin()
{
if (Session["LogedUserID"] != null)
{
return View();
}
else
{
return RedirectToAction("Index");
}
}
}
}
View Login Index
@model
LoginForm.Models.User
@{
ViewBag.Title = "Index";
}
<h2>Login</h2>
@using (Html.BeginForm("Index", "Login", FormMethod.Post))
{
//this is for create form tag
@Html.AntiForgeryToken() // this
is for prevent CSRF attack
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{
<div style="border:1px solid red">
@ViewBag.Message
</div>
}
<table>
<tr>
<td>@Html.LabelFor(a =>
a.UserName)</td>
<td>@Html.TextBoxFor(a =>
a.UserName)</td>
<td>@Html.ValidationMessageFor(a
=> a.UserName)</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.PassWord)
</td>
<td>
@Html.PasswordFor(a => a.PassWord)
</td>
<td>
@Html.ValidationMessageFor(a => a.PassWord)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Login" />
</td>
<td></td>
</tr>
</table>
}
@* This below line is for create javascript section *@
@section
Scripts{
@Scripts.Render("~/bundles/jqueryval")
@if (ViewBag.Message != null)
{
<script>
$(document).ready(function () {
alert('@ViewBag.Message');
});
</script>
}
After
Login View View>Login>AfterLogin
}
After Login
@model
LoginForm.Models.User
@{
ViewBag.Title = "AfterLogin";
}
<h2>AfterLogin</h2>
@if (Session["LogedUserFullname"] != null)
{
<text>
Welcome @Session["LogedUserFullname"].ToString()
</text>
}
Edmx…
User
//------------------------------------------------------------------------------
//
<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 LoginForm.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class User
{
public int UserId { get; set; }
[Required(ErrorMessage = "Please
provide username", AllowEmptyStrings = false)]
public string UserName { get; set; }
[Required(ErrorMessage = "Please
provide Password", AllowEmptyStrings = false)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
public string PassWord { get; set; }
//[Compare("Password",
ErrorMessage = "Confirm password dose not match.")]
//[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
//public
string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Please
provide full name", AllowEmptyStrings = false)]
public string FullName { get; set; }
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
ErrorMessage = "Please provide valid email id")]
public string EmailId { get; set; }
}
}