分享web开发知识

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

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

.NetCore中Session的使用

发布时间:2023-09-06 02:25责任编辑:熊小新关键词:暂无标签

在Core2.1中启用Session

Startup.cs文件进行配置

ConfigureServices方法的配置

在 services.AddMvc(....);这句上面加上

services.AddSession(options =>
???????????{
???????????????options.IdleTimeout = TimeSpan.FromMinutes(30);
???????????});

这里面的30是指Session的生命周期为30分钟

 ???????// This method gets called by the runtime. Use this method to add services to the container. ???????public void ConfigureServices(IServiceCollection services) ???????{ ???????????services.Configure<CookiePolicyOptions>(options => ???????????{ ???????????????// This lambda determines whether user consent for non-essential cookies is needed for a given request. ???????????????options.CheckConsentNeeded = context => true; ???????????????options.MinimumSameSitePolicy = SameSiteMode.None; ???????????}); ???????????services.AddSession(options => ???????????{ ???????????????options.IdleTimeout = TimeSpan.FromMinutes(30); ???????????}); ???????????services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ???????}

Configure方法中的配置

在 app.UseMvc(...);的上面加上

app.UseSession();

 ???????// 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(); ???????????} ???????????else ???????????{ ???????????????app.UseExceptionHandler("/Home/Error"); ???????????????app.UseHsts(); ???????????} ???????????app.UseHttpsRedirection(); ???????????app.UseStaticFiles(); ???????????app.UseCookiePolicy(); ???????????app.UseSession(); ???????????app.UseMvc(routes => ???????????{ ???????????????routes.MapRoute( ?????????????????name: "areas", ?????????????????template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ???????????????); ???????????????routes.MapRoute( ???????????????????name: "default", ???????????????????template: "{controller=Home}/{action=Index}/{id?}"); ???????????}); ???????}

在使用Session的页面中添加using引用

using Microsoft.AspNetCore.Http;

Microsoft.AspNetCore.Http的方法

namespace Microsoft.AspNetCore.Http{ ???public static class SessionExtensions ???{ ???????public static byte[] Get(this ISession session, string key); ???????public static int? GetInt32(this ISession session, string key); ???????public static string GetString(this ISession session, string key); ???????public static void SetInt32(this ISession session, string key, int value); ???????public static void SetString(this ISession session, string key, string value); ???}}

Get为获取值

HttpContext.Session.GetString("username");

Set为设置

HttpContext.Session.SetString("username", "admin");

.NetCore中Session的使用

原文地址:https://www.cnblogs.com/tangjiaoshu/p/10067884.html

知识推荐

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