惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
B
Blog RSS Feed
I
InfoQ
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
H
Help Net Security
L
LangChain Blog
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 牛腩

鸿蒙开发DevEco Studio创建hello world项目 SQL Server数据库中用存储过程来取顺序号 cmd命令行下用命令执行SQL脚本到SQL Server数据库中 Blazor项目中建立WebApi NETCORE下用SKIT类库发送微信模板消息 ASP.NET CORE微信支付回调示例代码 nuget离线安装 高德地图API使用(根据地址查经纬度,计算二个坐标之间的距离) ModernWMS - 仓库管理系统简记 .NET开源商城CoreShop简记 windows服务器上用nginx转发到iis中的网站 uniapp中防抖函数debounce的使用 winform中的picturebox控件显示和保存byte[]图片 ASP.NET CORE kindeditor在线编辑器示例(上传多图和插入VIDEO标签) uni app uview新增商品页(无限级分类选择和多图上传) C#获取HTML源码 Ant for Blazor做单个表的增删查改 ASP.NET MVC中使用Autofac依赖注入 NET8下生成二维码
NET 7 中使用Session
牛腩 · 2024-02-11 · via 博客园 - 牛腩

NET 7 中使用Session

2024年2月11日 net7下测试通过

Program.cs关键代码:

      builder.Services.AddDistributedMemoryCache();

        builder.Services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(30);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

        builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  //....
  
    app.UseSession();

母版页_Layout.cshtml中使用:

@using Microsoft.AspNetCore.Http
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor;

<div class="rtm r3">
 @if(_httpContextAccessor.HttpContext.Session.GetInt32("loginid") == null){
    <a href="javascript:;" class="rg1" onclick="login(2)">注册</a>
        <a href="javascript:;" class="rg2" onclick="login(1)">登录</a>
}
else
{
<span>欢迎您,
    @_httpContextAccessor.HttpContext.Session.GetString("loginname")
    ,</span>
<a href="/home/loginout" class="rg2">退出登录</a>
}
</div>

撸码:复制、粘贴,拿起键盘就是“干”!!!

posted @ 2024-02-11 20:24  牛腩  阅读(60)  评论()    收藏  举报