.NET WEBAPP中的Session

初学,写个小程序,数据库用ACCESS,后台是C#,ACTION全部用ASHX文件,前台用EXTJS,采用SESSION方法判断用户是否登录

用户登录时在SESSION中写入数据

context.Session["UserName"] = context.Request.Form["loginname"];

//数据库表中每条记录都有ROLEID,取数据时判断如select * from table where roleid=roleid
context.Session["RoleId"] = currole.ID;

//在主要的页面上作用户是否登录判断

context.Session["isLogin"] = true;

用户注销时将这些记录清空

由于老是要取且判断SESSION内容,于是写了个类来处理Session的内容,注意第一个方法最好用

/// <summary>
    /// 此类只包括静态函数,专门读取SESSION的内容
    /// </summary>
    public class SessionItems:IRequiresSessionState
    {
        public static object GetSessionObj(string SessionName)
        {
            if (HttpContext.Current.Session[SessionName] != null)
            {
                return HttpContext.Current.Session[SessionName];
            }
            return null;
        }
        public static string getSessionUserName()
        {
            if (HttpContext.Current.Session["UserName"] != null)
            {
                return (Convert.ToString(HttpContext.Current.Session["UserName"]));
            }            
            return null;
        }
        public static int getSessionRoleID()
        {
            if (HttpContext.Current.Session["RoleId"] != null)
            {
                return (Convert.ToInt32(HttpContext.Current.Session["RoleId"]));
            }    
            return 0;
        }
        public static bool getSessionIsLogin()
        {
            if (HttpContext.Current.Session["isLogin"] != null)
            {
                return (Convert.ToBoolean(HttpContext.Current.Session["isLogin"]));
            }    
            return false;
        }
    }

在ASHX中直接调用SessionItem类的静态方法

注意不管在哪个类中调用,那个类必须实现接口IRequiresSessionState,同时添加using System.Web.SessionState;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值