【问题标题】:check session in every page?检查每个页面中的会话?
【发布时间】:2013-03-27 15:20:55
【问题描述】:

我对 .NET 还是很陌生 - 我正在创建一个网站,该网站有一个只有登录用户才能看到的管理部分。 我已经创建了登录代码,一旦用户通过身份验证,我就会为他们分配一个会话变量。 我的问题是:有没有更有效的方法来检查会话变量,而不是在每个页面上都有以下功能?

protected void Page_Load(object sender, EventArgs e)
{
      checkSession();

 }
public void checkSession()
{
    if (Session["LoggedIn"] != "true")
    {
        Response.Redirect("default.aspx");
    }
}

谢谢!

【问题讨论】:

  • 你可以在一个类中拥有 checkSession() 函数,然后你可以调用 cls.checkSession() 并返回一个布尔值;
  • 你需要坚持使用Form Authentication,它使用cookie
  • 为什么是“真实的”?请改用 true。

标签: c# .net


【解决方案1】:

如果您使用MasterPage,您可以将检查代码放在MasterPage's Page_Load 事件中,如果不使用Global.asax 或自定义HttpModule,并将检查代码放在AcquireRequestState 事件处理程序中用于第一个,PostRequestHandlerExecute 用于第二个事件处理程序

Global.asax 示例

public class Global : System.Web.HttpApplication
{ ...
    void Application_AcquireRequestState(object sender, EventArgs e)
    {            
        HttpContext context = HttpContext.Current;
        // CheckSession() inlined
        if (context.Session["LoggedIn"] != "true")
        {
          context.Response.Redirect("default.aspx");
        }
    }
  ...
}

【讨论】:

  • ERR_TOO_MANY_REDIRECTS
  • 重定向到 default.aspx 时,仍然没有记录,所以再次重定向到 default.aspx
  • 我不同意将代码放入 MasterPage。如果 ContentPage(uses master) 需要验证怎么办?让我解释;想想 Profile.aspx 中使用 Master.site 的一些代码。加载 Profile.aspx 时,让页面需要 Session 值。但是,会话尚未控制。因为 Master.site 控制它。在这种情况下,Profile.aspx 中会引发 NullReference 异常。不要在主页面中检查会话。再见朋友/朋友
【解决方案2】:

您可以使您的页面成为一个继承自检查登录用户的基类的类。

【讨论】:

  • 我认为这是最好的方法。
【解决方案3】:

您可以创建 BasePage 并从该基本页面继承您的所有页面,在您的基本页面中设置功能。

public class BasePage : Page
{
   protected void checkSession()
   {
    if (Session["LoggedIn"] != "true")
    {
        Response.Redirect("default.aspx");
    }
   }
}

你的页面

public partial class YourPage : BasePage
{
....
}

另一种解决方案:

在您的 Http 模块中,创建 Principal(Roles) 和 Identity 以设置身份验证和授权功能,在您的 http 模块中将这些信息附加到当前线程。

链接:http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.isauthenticated.aspx

【讨论】:

    【解决方案4】:

    您或许应该考虑使用表单身份验证:
    http://www.asp.net/web-forms/videos/authentication/using-basic-forms-authentication-in-aspnet

    您可以将页面或文件夹配置为始终需要授权,因此运行时会处理该要求,而无需您手动检查。

    【讨论】:

    • 表单身份验证也是一个更全面的安全系统,与简单的会话检查相比,它具有几个优点,请参阅:support.microsoft.com/kb/910443
    【解决方案5】:

    从派生自 Page 的自定义类派生您的页面

    通过添加会话检查代码覆盖 Load 方法

    现在您的所有页面都已通过验证

    public class MyPage : System.Web.UI.Page
     protected void Page_Load(object sender, EventArgs e)
     {
       if (Session["yoursession"] != "true")
       {
         //code
       }
     }
    
    
    
    public class yourCustomPage1 : MyPage
    {   
     protected void Page_Load(object sender, EventArgs e)
     {
       //you don't have to check or call any method..
     }
    }
    
    public class yourCustomPage2 : MyPage
    {   
     protected void Page_Load(object sender, EventArgs e)
     {
       //you don't have to check or call any method..
     }
    }
    

    等等……

    【讨论】:

    • 有一个代码示例来说明这一点会很有用。
    【解决方案6】:

    开始了解 ASP.Net 中的表单身份验证的一个好方法是创建一个全新的网站。 进入 Visual Studio 并创建新项目,选择 Web,然后选择 ASP.NET Web 应用程序。 在 Account 文件夹中查看以了解流程和 ASP.Net 方法。

    【讨论】:

      【解决方案7】:
                  public class BasePage : Page
                  {
                   protected void checkSession()
                     {
                        if (Session["LoggedIn"] == null)
                         {
                                 Response.Redirect("~/default.aspx/");
                          }
                     }
                  }
      

      【讨论】:

        【解决方案8】:

        第一种方式:Global.asax.cs 添加

            void Application_AcquireRequestState(object sender, EventArgs e)
            {
                HttpContext context = HttpContext.Current;
                Page page = context.Handler as Page;
        
                if ((string)context.Session["LoggedIn"] != "true"
                    && !(page.AppRelativeVirtualPath == "~/Default.aspx"))
                    context.Response.Redirect("default.aspx");
            }
        

        第二种方式:您可以在母版页中进行相同的会话管理。页面加载事件。

        希望这会有所帮助。

        【讨论】:

          【解决方案9】:

          您可以在 GLOBAL.asax.cs 中添加此代码,这将检查会话是否为空,并且如果此请求是从登录页面初始化的,它也不会重定向,否则它将陷入循环。

           void Application_AcquireRequestState(object sender, EventArgs e)
              {
                  HttpContext context = HttpContext.Current;
                  // CheckSession() inlined
                  if (Context.Request.Url.LocalPath != "/UIComponents/User/Login.aspx")
                  {
                      if (context.Session["Name"] == null)
                      {
                          FormsAuthentication.RedirectToLoginPage();
                      }
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-11-02
            • 1970-01-01
            • 2013-10-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多