asp.net core中Microsoft.AspNet.Session的使用

时间:2021-02-08 05:31:05

1、通过nuget/修改project.json引用

2、引用存储session的媒介,如内存、数据库、redis等

    "Microsoft.AspNetCore.Session": "1.1.0-*",

    "Microsoft.Extensions.Caching.Memory": "1.1.0-*",

    "Microsoft.Extensions.Caching.Redis": "1.1.0-*",

    "Microsoft.Extensions.Caching.SqlServer": "1.1.0-*",

3、修改startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession();
services.AddMvc();
}
 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();

4、使用

Context.Session.GetString("UserName")

5、参考

示例:https://github.com/aspnet/Session/tree/dev/samples/SessionSample

仓库:https://github.com/aspnet/Session

相关文章