Managing a session is a common task in web applications. In this article I will show you how to handle sessions with custom attributes.
Scope
In most web applications we previously would keep the user information in the session after login. In some pages we use this session. Before using this session we need to check whether the session is null or not. If the session is null then it should redirect to the login page then after successful login the system should automatically redirect to the requested page.
Here is the code inside SessionExpire Attribute:
- public class SessionExpire : ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- if (HttpContext.Current.Session["UserInfo"] == null)
- {
- FormsAuthentication.SignOut();
- filterContext.Result =
- new RedirectToRouteResult(new RouteValueDictionary
- {
- { "action", "Index" },
- { "controller", "Login" },
- { "returnUrl", filterContext.HttpContext.Request.RawUrl}
- });
- return;
- }
- }
- }
Example
Here we have implemented SessionExpire in ManageAccountController.
- [SessionExpire]
- public class ManageAccountController : BaseController
- {
- public ActionResult Index()
- {
- return View();
- }
- }
- public class LoginController : BaseController
- {
- public ActionResult Index(string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- return View();
- }
- }
- @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) // in Index.cshtml
- [HttpPost]
- public ActionResult Index(Model objUser, string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- if (ModelState.IsValid)
- {
- FormsAuthentication.SetAuthCookie("Username", false);
- return RedirectToLocal(returnUrl);
- }
- else
- {
- return View();
- }
- }
- public ActionResult RedirectToLocal(string returnUrl)
- {
- if (Url.IsLocalUrl(returnUrl))
- {
- return Redirect(returnUrl);
- }
- else
- {
- return RedirectToAction("Index", "Home");
- }
- }


Kishan KushwahPosted Dec 7, 2017, 1:27 AM EST
Session web api in asp.net c# code demo
Kagiso MahlobogoanePosted Jun 30, 2016, 5:16 AM EDT
I see alll the controllers inherit from BaseController. Where is the code for that?
Vignesh ManiTOP 100Posted Mar 28, 2016, 11:57 AM EDT
Nice one
sushil kumarPosted Jan 30, 2016, 7:37 PM EST
This will not work in ajax requests
Former memberPosted Jun 29, 2015, 9:41 AM EDT
what this code is doing @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) ?
Rahul Kumar SaxenaTOP 100Posted Mar 28, 2015, 8:20 PM EDT
Good Show...
Gowtham RajamanickamTOP 100Posted Mar 28, 2015, 8:24 AM EDT
nice show..
Gowtham RajamanickamTOP 100Posted Mar 28, 2015, 8:23 AM EDT
nice..
Vijay SPosted Mar 27, 2015, 10:04 PM EDT
Nice
Tom MohanPosted Feb 28, 2015, 12:11 AM EST
Thanks Sourabh Somani
Sourabh SomaniTOP 100Posted Feb 27, 2015, 11:16 PM EST
Tom Mohan Nice starting... good article
Dinesh BeniwalTOP 10Posted Feb 25, 2015, 6:24 AM EST
Thanks for sharing, Welcome to C# Corner.
Sibeesh VenuTOP 100Posted Feb 25, 2015, 2:40 AM EST
Tom Mohan You are welcome.
Sibeesh VenuTOP 100Posted Feb 25, 2015, 12:15 AM EST
Good one.
Harpreet SinghPosted Feb 24, 2015, 7:25 PM EST
nice one
Rahul Kumar SaxenaTOP 100Posted Feb 24, 2015, 6:40 PM EST
helpful..