Hi All,
I am getting Too many redirects error in my MVC application.
I have kept the debugger in SessionExpire Attribute filter , here debugger goes in loop.
This error is occuring from implementation of Cookie in my project.
My requirement is to keep User session alive until user log out.
My Code:
Account Controller:
- [HttpGet]
- [AllowAnonymous]
- [SessionExpire]
- public ActionResult Login(string returnUrl)
- {
- HttpContext.Request.IsAjaxRequest();
- AccountModel userLoginModel = new AccountModel();
- string cookieName = "MyCookie";
- HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
- if (authCookie != null)
- {
- if (!string.IsNullOrEmpty(authCookie.Value))
- {
- FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
- if (authTicket.IsPersistent)
- {
- userLoginModel.UserName = authTicket.Name;
- userLoginModel.RememberMe = authTicket.IsPersistent;
- userLoginModel.Password = authTicket.UserData;
- }
- if (User.Identity.IsAuthenticated)
- {
- string UserID = GetLoggedInUserData();
-
- if (UserID != string.Empty)
- {
- System.Web.HttpContext.Current.Session["userID"] = UserID;
-
-
-
- }
- return RedirectToAction("Index", "Home");
- }
-
- }
- }
- return View();
- }
-
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Login(AccountModel oModel, string returnUrl)
- {
- string responseMessage = string.Empty;
- string UserName = oModel.UserName;
- string Password = oModel.Password;
-
- if (ValidateUser(UserName, Password, ref responseMessage))
- {
- FormsAuthentication.SetAuthCookie(oModel.UserName, oModel.RememberMe);
-
- int timeout = oModel.RememberMe ? 7 : 1;
- FormsAuthenticationTicket authTicket = new
- FormsAuthenticationTicket(1,
- oModel.UserName,
- DateTime.Now,
- DateTime.Now.AddDays(timeout),
- oModel.RememberMe,
- oModel.Password);
-
-
- string encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(authTicket);
- HttpCookie authCookie = new HttpCookie("MyCookie", encryptedTicket);
- if (oModel.RememberMe)
- {
- authCookie.Expires = authTicket.Expiration;
- }
- else
- { authCookie.Expires = authTicket.Expiration; }
-
-
- authCookie.HttpOnly = true;
- System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
- FormsIdentity identity = new FormsIdentity(authTicket);
- string UserID = GetLoggedInUserData();
- if (UserID != string.Empty)
- {
- System.Web.HttpContext.Current.Session["userID"] = UserID;
-
-
- }
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ViewBag.Error = true;
- ViewBag.Message = responseMessage;
- return View();
- }
- }
SessionExpire
- [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
- public class SessionExpireAttribute : ActionFilterAttribute
- {
-
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext context = HttpContext.Current;
- if (context.Session != null)
- {
- if (context.Session.IsNewSession == true)
- {
- string sessionCookie = context.Request.Headers["Cookie"];
-
- if ((sessionCookie != null) && (sessionCookie.IndexOf("ASP.NET_SessionId_My") >= 0))
- {
-
- string redirectTo = "~/Account/Login";
- if (!string.IsNullOrEmpty(context.Request.RawUrl))
- {
- filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl);
-
-
-
- return;
- }
-
- }
- }
- }
-
- base.OnActionExecuting(filterContext);
- }
- }