.net 5.0 - Session的使用
2021-05-30 16:02
标签:sso def 安装 get pad 使用 20px edm 设置 使用范例: .net 5.0 - Session的使用 标签:sso def 安装 get pad 使用 20px edm 设置 原文地址:https://www.cnblogs.com/gygtech/p/14669253.htmlMicrosoft.AspNetCore.Session
Microsoft.AspNetCore.Http.Extensions
// 添加一个内存缓存
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// 设置10秒钟Session过期来测试
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
});
app.UseSession();
HttpContext.Session.SetString("param", str);
string value = HttpContext.Session.GetString("param");
public class SessionTestClass
{
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession _session => _httpContextAccessor.HttpContext.Session;
public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void Set()
{
_session.SetString("code", "123456");
}
public void Get()
{
string code = _session.GetString("code");
}
}
public static class SessionExtensions
{
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObjectFromJson
var myTestObject = new MyTestClass();
HttpContext.Session.SetObjectAsJson("SessionTest", myTestObject);
var myComplexObject = HttpContext.Session.GetObjectFromJson
上一篇:HTML标签
文章标题:.net 5.0 - Session的使用
文章链接:http://soscw.com/index.php/essay/89608.html