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, 6:27 AM
Session web api in asp.net c# code demo
Kagiso MahlobogoanePosted Jun 30, 2016, 9:16 AM
I see alll the controllers inherit from BaseController. Where is the code for that?
Vignesh ManiPosted Mar 28, 2016, 3:57 PM
Nice one
sushil kumarPosted Jan 31, 2016, 12:37 AM
This will not work in ajax requests
Former memberPosted Jun 29, 2015, 1:41 PM
what this code is doing @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) ?
Rahul Kumar SaxenaPosted Mar 29, 2015, 12:20 AM
Good Show...
Gowtham RajamanickamPosted Mar 28, 2015, 12:24 PM
nice show..
Gowtham RajamanickamPosted Mar 28, 2015, 12:23 PM
nice..
Vijay SPosted Mar 28, 2015, 2:04 AM
Nice
Tom MohanPosted Feb 28, 2015, 5:11 AM
Thanks Sourabh Somani
Sourabh SomaniPosted Feb 28, 2015, 4:16 AM
Tom Mohan Nice starting... good article
Dinesh BeniwalPosted Feb 25, 2015, 11:24 AM
Thanks for sharing, Welcome to C# Corner.
Sibeesh VenuPosted Feb 25, 2015, 7:40 AM
Tom Mohan You are welcome.
Sibeesh VenuPosted Feb 25, 2015, 5:15 AM
Good one.
Harpreet SinghPosted Feb 25, 2015, 12:25 AM
nice one
Rahul Kumar SaxenaPosted Feb 24, 2015, 11:40 PM
helpful..