Introduction :
Here in this tutorial explain how pass session in defferent pages and expire session in mvc4 and also explain how to avoid backbutton problem after the session is expire.
Tools: Visual Studio 2012
First create model and add the class as the following
Now we can add controller and add two action method for login and logout here in logout controller we can add some code for expire the session. here using the abondom() session are expire and we ca use session.expire() and session.clear() method.
Now we can add view and display user name using the session and also add the logout button as the given below code.
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
// using this code we can display session name where we want
<text>
WelCome : @Session["SData"]
</text>
</section>
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
// here we can add the logout action methode to redirect the login page and session are expire
<li><a href="@Url.Action("SignOut", "ABC")" value="LogOut"/> LogOut </a><li>
</ul>
</nav>
</div>
In Global.asax.cs we can need to add the following code for expire the session globally in the application
In project Global.asax.cs file add below code
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
By adding the above code in global.asax.cs file it can clear the cache of the browser thus history of the browser completelly remove the session data and it can avoid the browser backbutton problem when user are logout from the system.
If you have any query reguarding the above code than comment the below comment box.
Here in this tutorial explain how pass session in defferent pages and expire session in mvc4 and also explain how to avoid backbutton problem after the session is expire.
Tools: Visual Studio 2012
First create model and add the class as the following
public class DAL
{
public int id { get; set; }
public string Name { get; set; }
public string pwd { get; set; }
public SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename);
public DataTable Login_data(DAL da)
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_user where Name='"+da.Name+"' and City='"+da.City+"'",con);
adp.Fill(dt);
return dt;
}
{
public int id { get; set; }
public string Name { get; set; }
public string pwd { get; set; }
public SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename);
public DataTable Login_data(DAL da)
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_user where Name='"+da.Name+"' and City='"+da.City+"'",con);
adp.Fill(dt);
return dt;
}
Now we can add controller and add two action method for login and logout here in logout controller we can add some code for expire the session. here using the abondom() session are expire and we ca use session.expire() and session.clear() method.
public ActionResult Loginview(DAL da)
{
DataTable dt = new DataTable();
dt = da.Login_data(da);
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["Name"].ToString() == da.Name && dr["City"].ToString() == da.City)
{
Session["SData"] = dr["Name"].ToString();
return RedirectToAction("Home"); //here user redirect the home page when login are done
}
else
{
ViewBag.Message = "Name and City not match ??????";
}
}
}
return View();
}
// when user can redirect to home page we need to check the session is expire or not using the if else statement if session are expire then it not give the permission to enter the login page without entering the user name and password
public ActionResult SignOut()
{
Session.Clear();
Session.Abandon();
Session.RemoveAll();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
System.Web.Security.FormsAuthentication.SignOut();
Session["SData"] = null;
return RedirectToAction("Loginview", "ABC");
}
{
DataTable dt = new DataTable();
dt = da.Login_data(da);
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["Name"].ToString() == da.Name && dr["City"].ToString() == da.City)
{
Session["SData"] = dr["Name"].ToString();
return RedirectToAction("Home"); //here user redirect the home page when login are done
}
else
{
ViewBag.Message = "Name and City not match ??????";
}
}
}
return View();
}
// when user can redirect to home page we need to check the session is expire or not using the if else statement if session are expire then it not give the permission to enter the login page without entering the user name and password
public ActionResult SignOut()
{
Session.Clear();
Session.Abandon();
Session.RemoveAll();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
System.Web.Security.FormsAuthentication.SignOut();
Session["SData"] = null;
return RedirectToAction("Loginview", "ABC");
}
Now we can add view and display user name using the session and also add the logout button as the given below code.
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
// using this code we can display session name where we want
<text>
WelCome : @Session["SData"]
</text>
</section>
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
// here we can add the logout action methode to redirect the login page and session are expire
<li><a href="@Url.Action("SignOut", "ABC")" value="LogOut"/> LogOut </a><li>
</ul>
</nav>
</div>
In Global.asax.cs we can need to add the following code for expire the session globally in the application
In project Global.asax.cs file add below code
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
By adding the above code in global.asax.cs file it can clear the cache of the browser thus history of the browser completelly remove the session data and it can avoid the browser backbutton problem when user are logout from the system.
If you have any query reguarding the above code than comment the below comment box.
Sign up here with your email
1 comments:
Write commentsthis is a very working coding
Replythnks to dotnetcode2u.com
ConversionConversion EmoticonEmoticon