分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

Asp.net core如何使用Session

发布时间:2023-09-06 01:44责任编辑:郭大石关键词:暂无标签

Asp.net core使用session:

在新建Asp.net core应用程序后,要使用session中间件,在startup.cs中需执行三个步骤:

1.使用实现了IDistributedcache接口的服务来启用内存缓存。(例如使用内存缓存)

 //该步骤需在addsession()调用前使用。 

2.调用addsession方法

3.使用usesession回调(usesession需在useMvc()方法前调用)

具体代码如下:

public class Startup ???{ ???????public Startup(IConfiguration configuration) ???????{ ???????????Configuration = configuration; ???????} ???????public IConfiguration Configuration { get; } ???????// This method gets called by the runtime. Use this method to add services to the container. ???????public void ConfigureServices(IServiceCollection services) ???????{ ???????????services.AddDistributedMemoryCache(); ???????????services.AddSession(options => ???????????{ ???????????????options.Cookie.Name= ".AdventureWorks.Session"; ???????????????options.IdleTimeout = TimeSpan.FromSeconds(10);//设置session的过期时间 ???????????????options.Cookie.HttpOnly = true; ???????????} ???????????); ???????????services.AddMvc(); ???????} ???????// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. ???????public void Configure(IApplicationBuilder app, IHostingEnvironment env) ???????{ ???????????if (env.IsDevelopment()) ???????????{ ???????????????app.UseDeveloperExceptionPage(); ???????????} ???????????app.UseSession(); ???????????app.UseMvc(); ???????}}

Asp.net core如何使用Session

原文地址:https://www.cnblogs.com/Pzhenzhen/p/8524864.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved