如何在会话超时时立即将用户重定向到登录页面(Default.aspx)

时间:2014-03-22 08:53:30

标签: c# asp.net session redirect

如果会话超时,我想重定向用户。在这里,我创建一个Web用户控件,在没有会话的情况下获取会话上下文,然后重定向到登录页面。

以下是网络用户控制代码:

 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);
    }
}

这是我的注册方法,这个Web用户控件对主页的所有页面生效都是这样的:

<%@ Register Src="~/Admin/WebUserControl.ascx" TagName="companylogo" TagPrefix="uc1"%>

此处出现问题这不正常。但是,当会话过期时,它不会将用户重定向到登录页面,但它会给出错误,如:

Server Error in '/EasyWeb' Application.

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the 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

但是,如果Session [“Username”]的默认时间已过期,则会出现此错误。但是这种Web用户控制机制不起作用(但它在过去有效)。也许我的代码出了问题?

这适用于我的网络安全,因为这种机制对于保护网站免受其他人的匿名访问非常有用。

0 个答案:

没有答案