System.Web.Security.FormsAuthentication.RedirectToLoginPage(); in Session_end of global.asax. Giving me the problem of "Object reference not set to instance"
using System;
using System.Web;
namespace WEI.ECommerce.Web
{
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Administrator will only be allowed a certain number of login attempts
Session["MaxLoginAttempts"] = 3;
Session["LoginCount"] = 0;
// Track whether they're logged in or not
Session["LoggedIn"] = "No";
// Track whether Admin logged in or not
Session["AdminLoggedIn"] = "No";
//if (Session.Keys.Count > 0)
//{
// System.Web.Security.FormsAuthentication.SignOut();
// System.Web.Security.FormsAuthentication.RedirectToLoginPage();
//}
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
System.Web.Security.FormsAuthentication.SignOut();
System.Web.Security.FormsAuthentication.RedirectToLoginPage();
}
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg.ToLower() == "culture")
{
if (context.Session["CultureName"] != null)
return context.Session["CultureName"].ToString();
}
return base.GetVaryByCustomString(context, arg);
}
}
}
System.Web.Security.FormsAuthentication.RedirectToLoginPage(); in Session_end of global.asax. Giving me the problem of "Object reference not set to instance"
First of all, Forms authentication has nothing to do with Session! Forms authentication uses cookies, not session
Session_end in Global.asax is not attached to any Request, it is a process that runs on the server, even when the user already closed his browser of is disconnected, so you cannot access the authentication cookie, resulting in this error
Thanks for your information . But my questioned need to be answered . I want to redirect to login page after session expires without refreshing the page.
Thanks for your information . But my questioned need to be answered . I want to redirect to login page after session expires without refreshing the page.
Bottom line is that you cannot do this using ASP.NET (which runs on the server). You need something (like javascript) that runs on the client, which will do the redirect or a meta redirect.
Be aware that the web is stateless!
Also, I already explained that forms authentication has nothing to do with session. So how do you authenticate your users, using Forms authenticaton or using Session?
Is there any example in Javascript to solve my problem.
Lets us identify your problem firts!? As I said, forms authentication had nothing to do with session, so why do you want to redirect to the login page when the session ends?
325 is number of seconds after page will automatic refresh.If it will auto refresh on browser and session has ended on server side then will be redirected to login page.
Note: if you are putting 300 seconds(means 5 mins) on this tag you have to code appropirate on server side to expire session in 5 mins. As many friends suggested you above.
shirishmanda
Member
4 Points
48 Posts
Redirect to login page when session expires
May 04, 2012 07:18 AM|LINK
I want to redirect to login page from other page after session ends, without doing refresh.
I tried out doing
System.Web.Security.FormsAuthentication.SignOut();
System.Web.Security.FormsAuthentication.RedirectToLoginPage(); in Session_end of global.asax. Giving me the problem of "Object reference not set to instance"
suggest me the solution.
Regards,
Shirish .M
Xsrk
Member
46 Points
35 Posts
Re: Redirect to login page when session expires
May 04, 2012 07:20 AM|LINK
can you post code of your Global.asax file
shirishmanda
Member
4 Points
48 Posts
Re: Redirect to login page when session expires
May 04, 2012 07:27 AM|LINK
using System; using System.Web; namespace WEI.ECommerce.Web { public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Administrator will only be allowed a certain number of login attempts Session["MaxLoginAttempts"] = 3; Session["LoginCount"] = 0; // Track whether they're logged in or not Session["LoggedIn"] = "No"; // Track whether Admin logged in or not Session["AdminLoggedIn"] = "No"; //if (Session.Keys.Count > 0) //{ // System.Web.Security.FormsAuthentication.SignOut(); // System.Web.Security.FormsAuthentication.RedirectToLoginPage(); //} } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. System.Web.Security.FormsAuthentication.SignOut(); System.Web.Security.FormsAuthentication.RedirectToLoginPage(); } public override string GetVaryByCustomString(HttpContext context, string arg) { if (arg.ToLower() == "culture") { if (context.Session["CultureName"] != null) return context.Session["CultureName"].ToString(); } return base.GetVaryByCustomString(context, arg); } } }hans_v
All-Star
35986 Points
6550 Posts
Re: Redirect to login page when session expires
May 04, 2012 08:35 AM|LINK
First of all, Forms authentication has nothing to do with Session! Forms authentication uses cookies, not session
Session_end in Global.asax is not attached to any Request, it is a process that runs on the server, even when the user already closed his browser of is disconnected, so you cannot access the authentication cookie, resulting in this error
shirishmanda
Member
4 Points
48 Posts
Re: Redirect to login page when session expires
May 04, 2012 09:11 AM|LINK
Thanks for your information . But my questioned need to be answered . I want to redirect to login page after session expires without refreshing the page.
Gaspard
Contributor
2066 Points
416 Posts
Re: Redirect to login page when session expires
May 04, 2012 09:27 AM|LINK
Maybe this thread can help
http://stackoverflow.com/questions/484964/asp-net-push-redirect-on-session-timeout
Regards
hans_v
All-Star
35986 Points
6550 Posts
Re: Redirect to login page when session expires
May 04, 2012 09:43 AM|LINK
Bottom line is that you cannot do this using ASP.NET (which runs on the server). You need something (like javascript) that runs on the client, which will do the redirect or a meta redirect.
Be aware that the web is stateless!
Also, I already explained that forms authentication has nothing to do with session. So how do you authenticate your users, using Forms authenticaton or using Session?
shirishmanda
Member
4 Points
48 Posts
Re: Redirect to login page when session expires
May 04, 2012 11:09 AM|LINK
hans_v,
I am using FormAuthentication to authenticate user , using
FormsAuthentication.SetAuthCookie(txtEmailAddress.Text, false);
Is there any example in Javascript to solve my problem.
hans_v
All-Star
35986 Points
6550 Posts
Re: Redirect to login page when session expires
May 04, 2012 11:18 AM|LINK
Lets us identify your problem firts!? As I said, forms authentication had nothing to do with session, so why do you want to redirect to the login page when the session ends?
mishra.bhupe...
Participant
1598 Points
378 Posts
Re: Redirect to login page when session expires
May 04, 2012 11:27 AM|LINK
I think you want to do that on client side:
Add following tag under yr head tag:(Master page or in all pages you want to auto redirect)
<META HTTP-EQUIV="Refresh" CONTENT="325;URL=../Login.aspx">
325 is number of seconds after page will automatic refresh.If it will auto refresh on browser and session has ended on server side then will be redirected to login page.
Note: if you are putting 300 seconds(means 5 mins) on this tag you have to code appropirate on server side to expire session in 5 mins. As many friends suggested you above.