TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Pravin Ghadge
563
2.1k
586.7k
Too many redirects error in MVC application
Apr 17 2018 1:25 AM
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();
//List<clsEntity_User> UserEntity = GetLoggedInUserData();
if
(UserID !=
string
.Empty)
{
System.Web.HttpContext.Current.Session[
"userID"
] = UserID;
//string userID = UserEntity[0].UserID.ToString();
//SessionValue.SetLoginData(userID, userRole, userEmail, userBusinessGroupIDs, userLocationIDs);
//UserEntity = null;
}
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,
//version
oModel.UserName,
// user name
DateTime.Now,
//creation
DateTime.Now.AddDays(timeout),
//Expiration (you can set it to 1 month
oModel.RememberMe,
//Persistent
oModel.Password);
// additional informations
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;
//SessionValue.SetLoginData(userID, userRole, userEmail, userBusinessGroupIDs, userLocationIDs);
//UserEntity = null;
}
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))
{
// FormsAuthentication.SignOut();
string
redirectTo =
"~/Account/Login"
;
if
(!
string
.IsNullOrEmpty(context.Request.RawUrl))
{
filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl);
//redirectTo = string.Format("~/Home/Login?ReturnUrl={0}", HttpUtility.UrlEncode(context.Request.RawUrl));
// filterContext.Result = new RedirectResult(redirectTo);
return
;
}
}
}
}
base
.OnActionExecuting(filterContext);
}
}
Attachment:
SessionWithCookie.zip
Reply
Answers (
2
)
Error In Asp.NET MVC5 Entity Framwork 6 and Stored Procedure
how to connect with masterpage and other webpages in asp.net