如何写好整站通用Session

你开发的网站或是应用程序,少不了使用Session.其中有几个Session一定是贯通整站的,如判断是否登录成功,记录被验证成功的帐号等。比如在需要需要登录成功之后才可以访问的页面的Page_load都要写一个判断if (Session["LoginOK"] xxx || Session["LoginOK"] xxx)等等。如何才可以做到化繁为简,化长为短呢?

可以尝试把这个些贯通整站的Session写在一个Biz的类别中,当然下面这个类别你还是可以扩展的。

Biz.cs:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BizBase
/// </summary>
namespace Insus.NET
{
    
public class Biz
    {
        
public Biz()
        {
            
//
            
// TODO: Add constructor logic here
            
//
        }

        
//存储判断是否成功登录
        public static bool LoginOk
        {
            
get
            {
                
return (HttpContext.Current.Session["LoginOk"!= null && (bool)HttpContext.Current.Session["LoginOk"]);
            }
            
set
            {
                HttpContext.Current.Session[
"LoginOk"= value;
            }
        }

        
//存储登录验证成功之后的Account
        public static string Account
        {
            
get
            {
                
return (HttpContext.Current.Session["Account"!= null ? HttpContext.Current.Session["Account"].ToString() : string.Empty);
            }
            
set
            {
                HttpContext.Current.Session[
"Account"= value;
            }
        }

        
//存储用户登录成功之后目标去向。
        public static string TargetPath
        {
            
get
            {
                
return (HttpContext.Current.Session["TargetPath"!= null ? HttpContext.Current.Session["TargetPath"].ToString() : VirtualPathUtility.ToAbsolute("~/Default.aspx"));
            }
            
set
            {
                HttpContext.Current.Session[
"TargetPath"= value;
            }
        }
    }
}

 

写好这个Biz类之后,你可以在登录页面的登录按钮使用上面Session:

View Code
 //登录按钮
    protected void ButtonLogining_Click(object sender, EventArgs e)
    {
        
//if (xxx) 如果验证成功
        {
            Biz.LoginOk 
= true;  //登录成功设为True.
            Biz.Account = "xxx";  //记录登录成功的帐号
            Response.Redirect(Biz.TargetPath);  //转向目标页面
        }
    }

 

到此为止,其本上算写好了。你想在任何一个页面取到用户登录的帐号,就可以如下取得:

string currentAccount = Biz.Account;

 

接下来,Insus.NET要说的是,用户如果一开始使用匿名访问网站,然后点击目标页面需要登录成功才能访问的链接,用户登录成功之后,能导向此页。又需要怎样实现呢?

这样情况,你需要改一下你的站点,除了login页面和Logout页面外,所有页面的Page_load添加一句代码: 

View Code
protected void Page_Load(object sender, EventArgs e)
    {
       
//如果此页面需要在登录验证成功之后,直接导向此页。
        Biz.TargetPath = Request.RawUrl;
    }

 

还有一个问题,你的站点所有的网页面中,一些页面是需要登录才可以访问,一些需要匿名即可访问,那又要怎样控制与判断当前用户是否已经登录成功呢?解决这个问题,你可以写一个类别。如BasePage类,这个类别需要继承: System.Web.UI.Page ,这个类别中,写一个判断是否为成功登录方法:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BasePage
/// </summary>
namespace Insus.NET
{
    
public class BasePage : System.Web.UI.Page
    {
       
//此方法判断是否登录
        public void LoginAuthorizationed()
        {
            
if (!Biz.LoginOk) //成功能登录
            {
                HttpContext.Current.Response.Redirect(Biz.TargetPath);  
//导向目录页面。
            }
        }
    }
}

 

在写到需要验证的那些页面再继承这个BasePage类别即可,如:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class _Default : BasePage //继承BasePage
{
    
protected void Page_Load(object sender, EventArgs e)
    {
       
//如果此页面需要在登录验证成功之后,直接导向此页。
        Biz.TargetPath = Request.RawUrl;

        
//需要登录才能访问的页面,在Page_load放下面这个方法。
        LoginAuthorizationed();
    }
}

 

posted @ 2011-04-29 11:05  Insus.NET  阅读(8609)  评论(4编辑  收藏  举报