c# - How to redirect immediately user to login page (Default.aspx) on session time out -
i want redirects user if session time outs. here make 1 web user control fetches session context in no session exist it's redirects login page.
here web user control code:
public partial class webusercontrol : system.web.ui.usercontrol { protected override void onprerender(eventargs e) { if (context.session["username"] != null && context.session.isnewsession) { if (this.page.user != null && this.page.user.identity.isauthenticated) { formsauthentication.signout(); response.redirect("../default.aspx"); } } base.onprerender(e); } }
and register method web user control take effect pages master page like:
<%@ register src="~/admin/webusercontrol.ascx" tagname="companylogo" tagprefix="uc1"%>
here problem occurs not works fine. when session expired it's not redirect user login page it's give error :
server error in '/easyweb' application. sequence contains no elements description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.invalidoperationexception: sequence contains no elements source error: line 53: using (dataclassesdatacontext db = new dataclassesdatacontext()) line 54: { line 55: int user_id = db.users.where(u => u.username.equals((string)session["username"])).select(u=>u.id).first(); line 56: foreach (var item in db.users.where(u=>u.id.equals(user_id)).select(u=>u).tolist()) line 57: { source file: f:\easyweb\admin\masterpage.master.cs line: 55 stack trace: [invalidoperationexception: sequence contains no elements] system.data.linq.sqlclient.sqlprovider.execute(expression query, queryinfo queryinfo, iobjectreaderfactory factory, object[] parentargs, object[] userargs, icompiledsubquery[] subqueries, object lastresult) +847 system.data.linq.sqlclient.sqlprovider.executeall(expression query, queryinfo[] queryinfos, iobjectreaderfactory factory, object[] userarguments, icompiledsubquery[] subqueries) +113 system.data.linq.sqlclient.sqlprovider.system.data.linq.provider.iprovider.execute(expression query) +344 system.data.linq.dataquery`1.system.linq.iqueryprovider.execute(expression expression) +23 system.linq.queryable.first(iqueryable`1 source) +211 admin_masterpage.page_load(object sender, eventargs e) in f:\easyweb\admin\masterpage.master.cs:55 system.web.util.callihelper.eventargfunctioncaller(intptr fp, object o, object t, eventargs e) +14 system.web.util.callieventhandlerdelegateproxy.callback(object sender, eventargs e) +35 system.web.ui.control.onload(eventargs e) +99 system.web.ui.control.loadrecursive() +50 system.web.ui.control.loadrecursive() +141 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +627 version information: microsoft .net framework version:2.0.50727.3053; asp.net version:2.0.50727.3053
however error raise when session["username"] has been expired default time. web user control mechanism not works (however worked in past). maybe there wrong code?
it's web security because mechanism useful protecting web site anonymous access others.
Comments
Post a Comment