如何写好整站通用Session
来源:互联网 发布:企业样本设计制作软件 编辑:程序博客网 时间:2024/05/16 10:23
你开发的网站或是应用程序,少不了使用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;
}
}
}
}
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); //转向目标页面
}
}
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;
}
{
//如果此页面需要在登录验证成功之后,直接导向此页。
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); //导向目录页面。
}
}
}
}
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();
}
}
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();
}
}
- 如何写好整站通用Session
- 如何处理一个站点不同Web应用通用Session
- 团队开发中,如何处理一个站点不同Web应用通用Session
- 团队开发中,如何处理一个站点不同Web应用通用Session (转载)
- 团队开发中,如何处理一个站点不同Web应用通用Session
- Asp.net中如何处理一个站点不同Web应用通用Session的问题
- 团队开发中,如何处理一个站点不同Web应用通用Session
- 团队开发中,如何处理一个站点不同Web应用通用Session
- 团队开发中,如何处理一个站点不同Web应用通用Session
- 团队开发中通用Session
- 如何设计通用权限
- 如何造一支通用触控笔?
- 什么是Session 如何使用Session
- 如何结束session?
- Spring如何管理Session
- 如何使用 session
- oracle 如何杀掉session
- HttpWebRequest 如何保留 Session
- Http和Socket连接区别
- 内存泄漏检测利器 - Visual Leak Detector
- 多实践,多回顾
- 使用GetValue和SetValue来设置属性
- Android启动模式对activity行为的影响
- 如何写好整站通用Session
- Linux下spi驱动开发(1)
- OpenLayers深入浅出(3)----定制绘图对象及其相关操作
- ADO.NET Entity Framework 深入分析, Part 6 – 处理并发(Concurrency Handling)
- Cybertecture打造的增强现实魔镜,天朝也能买到
- (30)HttpServletRequest对象和应答HttpServletResponse对象以及动态文件的下载
- MTK 11A窗体与类
- apache切割日志
- 判断 是否 出现滚动条