You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

43 lines
1.4 KiB

/*******************************************************************************
* Copyright © 2017-2020 Znyc.Cloudcar.Admin.Framework 版权所有
* Author: Znyc
* Description: Znyc快速开发平台
* Website:http://www.Znyc.Cloudcar.Admin.com
*********************************************************************************/
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace Znyc.Cloudcar.Admin.Commons.Extensions
{
/// <summary>
/// Session 扩展函数,直接将实体类序列化成json存储和读取
/// </summary>
public static class SessionExtensions
{
/// <summary>
/// 设置session值
/// </summary>
/// <param name="session"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
/// <summary>
/// 获取session
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="session"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(this ISession session, string key)
{
string value = session.GetString(key);
return value == null ? default : JsonConvert.DeserializeObject<T>(value);
}
}
}