asp.Net自定义Session管理器_MVC Session实例程序
2014-07-07 19:40:52 By: shinyuu
介绍
在Web应用程序中,我们使用Session、检查用户是否登录、或保存权限信息和保存临时数据
有的时候,我们可能需要经常使用保存的会话对象。
在这里,我们将对 Session 的操作全部封装到我们的会话 Controller 里面
这样我们就可以使用这个 Controller 来控制器会话对象。
背景
在开始之前,我们必须考虑一些事情,比如:
我们将使用一个会话为完整的Web项目,这是一个很好的做法。
如果 Controller 是依赖 Session 和 Session 是否为空,我们将重定向到登录页面。
不是所有的控制器都会话相关的,比如 LogOnController 和 ErrorController。
所以,如果会话为 null,它不会重定向到登录页面,而且会令它的默认值。
使用代码
下面是我们的应用程序的基本控制,其中涉及的会话工具。
来源是我们要在保存到会话中的模型类型。现在,我们可以以两种方式使用它。
如果 Controller 不和 Session 相关的话,我们只是避免了继承。
如果 Controller 不和 Session 相关的,但我们继承 Controller。然后 IsNonsessionController 方法 nonsessionedController 名单是很重要的,
在这里我们要指定控制器的名称不依赖于会话:
public class ApplicationController<TSource> : Controller { //session index name private const string LogOnSession = "LogOnSession"; //session independent controller private const string ErrorController = "Error"; //session independent and LogOn controller private const string LogOnController = "LogOn"; //action to rederect private const string LogOnAction = "LogOn"; protected ApplicationController() { } protected override void Initialize(RequestContext requestContext) { base.Initialize(requestContext); //important to check both, because logOn and error //Controller should be access able with out any session if (!IsNonSessionController(requestContext) && !HasSession()) { //Rederect to logon action Rederect(requestContext, Url.Action(LogOnAction, LogOnController)); } } private bool IsNonSessionController(RequestContext requestContext) { var currentController = requestContext.RouteData.Values["controller"] .ToString().ToLower(); var nonSessionedController = new List<string>() { ErrorController.ToLower(), LogOnController.ToLower() }; return nonSessionedController.Contains(currentController); } private void Rederect(RequestContext requestContext, string action) { requestContext.HttpContext.Response.Clear(); requestContext.HttpContext.Response.Redirect(action); requestContext.HttpContext.Response.End(); } protected bool HasSession() { return Session[LogOnSession] != null; } protected TSource GetLogOnSessionModel() { return (TSource)this.Session[LogOnSession]; } protected void SetLogOnSessionModel(TSource model) { Session[LogOnSession] = model; } protected void AbandonSession() { if (HasSession()) { Session.Abandon(); } } }这里LogOnModel是哪个对象可以在Session 中设置的模式。并在每个Controller 中,我们将使用主控制器一样,如果控制器要处理的会话。
public class LogOnController : ApplicationController<LogOnModel> { }设置 Session,或者毁灭 Session,或从任何子 Controller 获取 Session,我们使用:
/*Set model to session*/ LogOnModel model = new LogOnModel(); SetLogOnSessionModel(model); /*destroy current session*/ AbandonSession(); /*Shows the session*/ LogOnModel sessionModel = GetLogOnSessionModel();限制
在这里,我们讲解的只有单个 Session。但是你的应用程序可能需要处理多个 Session。
要做到这一点,我们必须在现有的代码上做出一些改变。
若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)
或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)
如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教
为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)
感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛