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")
视图中使用
string str = Microsoft.AspNetCore.Http.SessionExtensions.GetString(Context.Session,"User");