C#获取Session对象

protected void Page_Load(object sender, EventArgs e)
    {
        YF.Model.Admin myuser = new YF.Model.Admin();

        //myuser = YF.SessionHelper.GetSession("user");不能这样写,需要用如下方法强制转换

        myuser = (YF.Model.Admin)YF.SessionHelper.GetSession("user");

        if (myuser != null)
        {
            this.username.Text = myuser.Username;
        }
        else
        {
            YF.JsHelper.AlertAndRedirect("尚未登陆", "/login.aspx");
        }
    }
namespace YF
{
    /// <summary>
    /// Session 操类
    /// 1、GetSession(string name)根据session名获取session对象
    /// 2、SetSession(string name, object val)设置session
    /// </summary>
    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
        /// </summary>
        /// <returns></returns>
        public static void ClearSession()
        {
            HttpContext.Current.Session.Clear();
        }

        /// <summary>
        /// 删除个指定的ession
        /// </summary>
        /// <param name="name">Session名称</param>
        /// <returns></returns>
        public static void RemoveSession(string name)
        {
            HttpContext.Current.Session.Remove(name);
        }

        /// <summary>
        /// 删除所有的ession
        /// </summary>
        /// <returns></returns>
        public static void RemoveAllSession(string name)
        {
            HttpContext.Current.Session.RemoveAll();
        }
    }
}

 

荐: C#获取Session对象

protected void Page_Load(object sender, EventArgs e) { YF.Model.Admin myuser = new YF.Model.Admin(); //myuser = YF.SessionHelper.G…

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /www/wwwroot/www.ksyll.com/wp-includes/meta.php on line 1206

Warning: error_log(/www/wwwroot/www.ksyll.com/wp-content/plugins/spider-analyser/#log/log-2406.txt): failed to open stream: Permission denied in /www/wwwroot/www.ksyll.com/wp-content/plugins/spider-analyser/spider.class.php on line 2984