In this article we will discuss about Sessions:
Firstly create Model, SignUp and Db Class:
SignUp Class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace Session.Models
{
[Table("tbl_account")]
public class SignUp
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string password { get; set; }
}
}
Db Class:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Session.Models
{
public class db:DbContext
{
public DbSet<SignUp> signups { get; set; }
}
}
Now Create our Controller and Code in it.
using Session.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Session.Controllers
{
public class HomeController : Controller
{
db obj = new db();
public ActionResult Index()
{
return View();
}
public ActionResult Signup()
{
return View();
}
[HttpPost]
public ActionResult Signup(SignUp model)
{
SignUp s = new SignUp();
s.name = model.name;
s.email = model.password;
s.password = model.password;
obj.signups.Add(s);
obj.SaveChanges();
return RedirectToAction("Signin");
}
public ActionResult Signin()
{
return View();
}
[HttpPost]
public ActionResult Signin(SignUp model)
{
SignUp s = obj.signups.Where(x => x.email.Equals(model.email) && x.password.Equals(model.password)).SingleOrDefault();
if (s!=null)
{
Session["Userid"] = s.id.ToString();
Session["UserEmail"] = s.email.ToString();
return RedirectToAction("UserDashboard");
}
return View();
}
public ActionResult UserDashboard()
{
return View();
}
public ActionResult Logout()
{
Session.Abandon();
FormsAuthentication.SignOut();
return RedirectToAction("Signin");
}
}
}
And now Create View:
Signin.cshtml (View)
@model Session.Models.SignUp
@{
ViewBag.Title = "Signin";
}
<h2>Signin</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SignUp</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Signup.cshtml (View)
@model Session.Models.SignUp
@{
ViewBag.Title = "Signup";
}
<h2>Signup</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SignUp</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
UserDashboard.cshtml (View)
@{
ViewBag.Title = "UserDashboard";
}
<h2>UserDashboard</h2>
@if (Session["UserEmail"]!=null)
{
<h1>Welcome @Session["UserEmal"].ToString()</h1>
}
else
{
Response.Redirect("~/Home/Signin");
}
Firstly create Model, SignUp and Db Class:
SignUp Class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace Session.Models
{
[Table("tbl_account")]
public class SignUp
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string password { get; set; }
}
}
Db Class:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Session.Models
{
public class db:DbContext
{
public DbSet<SignUp> signups { get; set; }
}
}
Now Create our Controller and Code in it.
using Session.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Session.Controllers
{
public class HomeController : Controller
{
db obj = new db();
public ActionResult Index()
{
return View();
}
public ActionResult Signup()
{
return View();
}
[HttpPost]
public ActionResult Signup(SignUp model)
{
SignUp s = new SignUp();
s.name = model.name;
s.email = model.password;
s.password = model.password;
obj.signups.Add(s);
obj.SaveChanges();
return RedirectToAction("Signin");
}
public ActionResult Signin()
{
return View();
}
[HttpPost]
public ActionResult Signin(SignUp model)
{
SignUp s = obj.signups.Where(x => x.email.Equals(model.email) && x.password.Equals(model.password)).SingleOrDefault();
if (s!=null)
{
Session["Userid"] = s.id.ToString();
Session["UserEmail"] = s.email.ToString();
return RedirectToAction("UserDashboard");
}
return View();
}
public ActionResult UserDashboard()
{
return View();
}
public ActionResult Logout()
{
Session.Abandon();
FormsAuthentication.SignOut();
return RedirectToAction("Signin");
}
}
}
And now Create View:
Signin.cshtml (View)
@model Session.Models.SignUp
@{
ViewBag.Title = "Signin";
}
<h2>Signin</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SignUp</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Signup.cshtml (View)
@model Session.Models.SignUp
@{
ViewBag.Title = "Signup";
}
<h2>Signup</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SignUp</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
UserDashboard.cshtml (View)
@{
ViewBag.Title = "UserDashboard";
}
<h2>UserDashboard</h2>
@if (Session["UserEmail"]!=null)
{
<h1>Welcome @Session["UserEmal"].ToString()</h1>
}
else
{
Response.Redirect("~/Home/Signin");
}
Comments
Post a Comment