public class CookieHelper : System.Web.SessionState.IReadOnlySessionState
    {

public static void SetCookies(HttpContext context, string cookieName, Hashtable hashtable)
        {
            HttpCookie myCookie  = new HttpCookie(cookieName);

foreach (DictionaryEntry de in hashtable)
            {
                myCookie.Values.Remove(de.Key.ToString());
                myCookie.Values.Add(de.Key.ToString(), de.Value.ToString());
            }
            context.Response.Cookies.Add(myCookie);
        }

public static void SetSession(HttpContext context, string sessionName, string UserInfoId)
        {
            context.Session[sessionName] = UserInfoId;
        }

public static string GetSession(HttpContext context, string sessionName)
        {
            try
            {
                if (context.Session != null)
                {
                    return context.Session[sessionName].ToString();
                }
                else
                {
                    return "null";
                }
            }
            catch (Exception)
            {

return "null";
            }
        }

public static void RemoveSession(HttpContext context, string sessionName)
        {
            if (context.Session != null)
            {
                context.Session.Remove(sessionName);
            }
        }

public static HttpCookie GetCookies(HttpContext context, string cookieName)
        {
            if (context.Request.Cookies[cookieName] != null)
            {
                HttpCookie myCookie = context.Request.Cookies[cookieName];
                return myCookie;
            }
            else
            {
                return null;
            }
        }

public static void SetCookie(HttpContext context,string cookieName, string cookieValue)
        {
            HttpCookie myCookie = new HttpCookie(cookieName);

myCookie.Value = context.Server.UrlEncode(cookieValue);

context.Response.Cookies.Add(myCookie);
        }

public static string GetCookie(string cookieName)
        {
            HttpContext context = HttpContext.Current;
            if (context.Request.Cookies[cookieName] != null)
                return context.Server.UrlDecode(context.Request.Cookies[cookieName].Value);
            else
                return string.Empty;
        }

public static void RemoveCookie(HttpContext context,string cookieName)
        {
            if (context.Request.Cookies[cookieName] != null)
            {
                SetCookie(context, cookieName, "");
            }
        }

}

封装对Cookie和Session设置或取值的类的更多相关文章

  1. cookie 和 session 设置

    cookie: 保存在浏览器上的一组键值对, 是由服务器让浏览器进行设置的 下次浏览器访问的时候会携带cookie. request是客户端请求, response是服务端响应. 读取客户端的cook ...

  2. Flask中cookie和session设置与csrf原理攻防

    Flask之操作cookie app.py from flask import Flask, request, Response app = Flask(__name__) @app.route('/ ...

  3. flask中cookie和session设置

    flask中cookie和session介绍   一.cookie: 在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户. ...

  4. Spring MVC 常用注解 和session界面渲染取值

    @RequestParams name 修饰当前形参的属性 value 和name属性一样 也是修饰当前属性 defaultValue 给属性设置一个默认值 默认属性 required 必备属性 1. ...

  5. js localStorage 设置和取值

    定义 Storage 对象,对象有get(取值), set(设置), add(加入新值)三个方法 const Storage = {} Storage.get = function (name) { ...

  6. web.config设置和取值

    博客园中有一篇文章对web.config的结构做了很详细的介绍,原文见 http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.htm ...

  7. jquery对strutrs2 <s:radio>标签的设置和取值

    今天郁闷了1小时. 需求是这样的: <s:radio  list="#{0:'男',1:'女'}" value="member.sex" id=" ...

  8. java 反射原理写了一个赋值和取值通用类

    首先了解一下反射的原理,什么是反射?所谓的反射就是指java 语言在运行时拥有一项自观的能力,反射能使你得到装载到 jvm 中的类的内部信息,它不需要你在编码的时候就知道所需类的内部信息,允许程序执行 ...

  9. falsk 与 django cookie和session存、取、删的区别

    falsk cookie的存取删需导入from flask import Flask,make_response,request# 存COOKIE的方法@app.route('/setcookie') ...

随机推荐

  1. asp.net 导入

    开发项目过程中会遇到各种各样的项目需求,我现在遇到的问题是每个部门有不同的excel文件类型,他们每个部门每个文件类型上传成功之后都会在数据库中产生表,表的列名是你excel第一行数据,其他行作为表的 ...

  2. tomcat架构分析-索引

    出处:http://gearever.iteye.com tomcat架构分析 (概览) tomcat架构分析 (容器类) tomcat架构分析 (valve机制) tomcat架构分析 (valve ...

  3. BTREE与其它索引的优缺点对比

    数据库BTree索引.Hash索引.Bitmap位图索引的优缺点 (2016-01-05 17:13:40) 转载▼ 标签: 数据库 索引 mysql oracle 分类: IT http://www ...

  4. Scut DirCenter 网站编辑、搭建与调试

    直接利用 Scut 提供的服务器管理工具进行服务器管理. 教程:https://github.com/ScutGame/Scut/wiki/DirServer. 几个注意点: 下载的数据库导入bat是 ...

  5. C++不能中断构造函数来拒绝产生对象(在构造和析构中抛出异常)

    这是我的感觉,具体需要研究一下- 找到一篇文章:在构造和析构中抛出异常 测试验证在类构造和析构中抛出异常, 是否会调用该类析构. 如果在一个类成员函数中抛异常, 可以进入该类的析构函数. /// @f ...

  6. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  7. 工资表的生成、显示、修改工资,应用transactionscope 分布式事务

    一: 二:SalarySheetDAL.cs using System; using System.Collections.Generic; using System.Linq; using Syst ...

  8. Xshell中文编码的设置

    一直用的是SSH Secure Shell Client,关于中文乱码,一直找不到简便的解决方案,后来改用XShell,编码设置如下: 1.查看linux系统编码,linux命令: locale. 2 ...

  9. 动态规划——G 回文串

    G - 回文串 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  10. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...