using System; using System.Collections.Generic; using System.Text; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Http; using NewLife.Log; namespace COMCMS.Common { public class SessionHelper { #region 获取Session /// /// 获取Session /// /// /// public static object GetSession(string strName) { byte[] result; try { MyHttpContext.Current.Session.TryGetValue(strName, out result); if (result == null || result.Length <= 0) return ""; object strResult = ByteConvertHelper.Bytes2Object(result); if (strResult == null) { return ""; } return strResult; } catch(Exception ex) { XTrace.WriteLine($"获取Session出错:{ex.Message}"); return ""; } } #endregion #region 写入Session /// /// 设置Session /// /// 名称 /// 值 public static void WriteSession(string strName, object value) { MyHttpContext.Current.Session.Set(strName, ByteConvertHelper.Object2Bytes(value)); } #endregion } }