为了账号安全,请及时绑定邮箱和手机立即绑定

如何写好整站通用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();
    }
}

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消