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

Asp.net core如何使用Session

时间:2018-03-07 21:46:19      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:gpo   develop   图片   方法   oid   mvc   led   eth   缓存   

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

标签:gpo   develop   图片   方法   oid   mvc   led   eth   缓存   

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

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