码迷,mamicode.com
首页 > Web开发 > 详细

.NetCore中Session的使用

时间:2018-12-05 00:04:23      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:dex   nbu   void   ONBUILD   启用   net   div   page   nbsp   

在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的使用

标签:dex   nbu   void   ONBUILD   启用   net   div   page   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!