public class SessionHelper
{
/// <summary>
/// 根据session名获取session对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static object GetSession(string name)
{
return HttpContext.Current.Session[name];
}
/// <summary>
/// 设置session
/// </summary>
/// <param name="name">session 名</param>
/// <param name="val">session 值</param>
public static void SetSession(string name, object val)
{
HttpContext.Current.Session.Remove(name);
HttpContext.Current.Session.Add(name, val);
}
/// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
public static void Add(string strSessionName, string strValue)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = ;
} /// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
public static void Adds(string strSessionName, string[] strValues)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = ;
} /// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Add(string strSessionName, string strValue, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = iExpires;
} /// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Adds(string strSessionName, string[] strValues, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = iExpires;
} /// <summary>
/// 读取某个Session对象值
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值</returns>
public static string Get(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName].ToString();
}
} public static void Set<T>(string strSessionName, T value) where T : class , new()
{
HttpContext.Current.Session[strSessionName] = value;
HttpContext.Current.Session.Timeout = * ;
} /// <summary>
/// 读取某个Session对象值
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值</returns>
public static T Get<T>(string strSessionName) where T : class , new()
{
if (HttpContext.Current == null || HttpContext.Current.Session == null || HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName] as T;
}
} /// <summary>
/// 读取某个Session对象值数组
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值数组</returns>
public static string[] Gets(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return (string[])HttpContext.Current.Session[strSessionName];
}
} /// <summary>
/// 删除某个Session对象
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
public static void Del(string strSessionName)
{
HttpContext.Current.Session[strSessionName] = null;
}
}

.NET Session操作的更多相关文章

  1. cookie编码乱码问题与cookie禁用后session操作

    Cookie传输的值只能是ASCII码,该编码表相对老旧不含有很多符号与文字 特别是中文,所以在cookie传值过程中需要先转成相应的ASCII编码再解析 如下 URLEncoder.encode(& ...

  2. C#语法糖之 session操作类 asp.net

    用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&quo ...

  3. session操作类

    using System;using System.Web; /// <summary> ///session操作类 /// </summary> public class a ...

  4. [py][mx]django的cookie和session操作-7天免登录

    浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...

  5. Python学习---django下的Session操作 180205

    和Cookie一样,都是用来进行用户认证.不同的是,Cookie可以吧明文/密文的信息都会KV返回给客户段,但是session可以吧用户的Value[敏感信息]保存在服务器端,安全. Django中默 ...

  6. TP框架中session操作

    TP中session操作 查看代码,OMG! 不应该是这样的

  7. Flask初学者:session操作

    cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...

  8. Flask中的session操作

    一.配置SECRET_KEY 因为flask的session是通过加密之后放到了cookie中.所以有加密就有密钥用于解密,所以,只要用到了flask的session模块就一定要配置“SECRET_K ...

  9. C# Session 操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. CI的session操作

    在使用session之前,要对配置文件config.php 里面的$config['encryption_key']随便赋个值,例如1234 1. 首先要加载session类,固定写法:$this-& ...

随机推荐

  1. Daas

    联想到这些年遇到的各种客户使用桌面虚拟化的场景,深有感触.桌面虚拟化技术并不一定适合所有的用户和场景,也不仅仅只是技术方面的问题.加强安全的方法有千万种,为何此客户情守桌面虚拟化呢?某客户已经规模化实 ...

  2. linux包之e2fsprogs之chattr命令

    概述 [root@localhost ~]# rpm -qf /usr/bin/chattre2fsprogs-1.41.12-18.el6.x86_64 chattr命令的作用很大,其中一些功能是由 ...

  3. eclipse开发Android程序sdk和avd的图标不见了

    在eclipse中开发我们的Android程序时,安装sdk是必不可少的,有时候会出现sdk和avd的图标都不见了的情况,一般出现这种情况的原因是你从别处copy了一个sdk的包然后直接在引用造成的, ...

  4. RMAN备份与恢复之不完全恢复

    要点:对于RMAN的不完全恢复,有如下步骤: 1)加载数据到mount状态(建议恢复前先做备份) 2)为高并发分配多个通道 3)还原所有(所需)的数据文件 4)使用until time,until s ...

  5. [java] 40个Java多线程问题总结

    40个问题汇总 1.多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓"知其然知其所以然","会用 ...

  6. [activiti] Activiti 5.18 的Mybatis版本依赖问题

    测试activiti 是查询Task时抛出一个异常: org.apache.ibatis.exceptions.PersistenceException: ### Error querying dat ...

  7. Struts2 Interceptors

    Alias Interceptor : 别名拦截器 <action name="someAction" class="com.examples.SomeAction ...

  8. Linux dirname $0 source if

    $SHELL gives the full path to your default shell. $0 gives the name of your current shell. dirname是一 ...

  9. 转:Why SeaJS

    原文地址:http://chaoskeh.com/blog/why-seajs.html Why SeaJS 前言 本文主要面向刚接触 SeaJS 的同学.文章会先提出传统 Javascript 开发 ...

  10. 【Log4j】 log4j.properties 使用

    一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session丢失 INFO 为一般要显示的信息,比如 ...