.net core 在ASP.NET Core 2.2中,HttpContext.Session为空
您好,我正在尝试将.Net Framework 4.6应用程序迁移到asp.Net core 2.2,我无法使用HttpContext.Session 我可以调用SetString方法,但是在第二个请求中,GetString总是返回null值 我尝试了Stackoverflow和官方文档中的不同答案,但没有一个能解决我的问题.net core 在ASP.NET Core 2.2中,HttpContext.Session为空,.net-core,session-state,httpcontext,asp.net-core-2.2,.net Core,Session State,Httpcontext,Asp.net Core 2.2,您好,我正在尝试将.Net Framework 4.6应用程序迁移到asp.Net core 2.2,我无法使用HttpContext.Session 我可以调用SetString方法,但是在第二个请求中,GetString总是返回null值 我尝试了Stackoverflow和官方文档中的不同答案,但没有一个能解决我的问题 public void ConfigureServices(IServiceCollection services) { var appCo
public void ConfigureServices(IServiceCollection services)
{
var appConfiguration = new AppConfigurationManager(Configuration);
var allowedOrigins = appConfiguration.AllowedOrigins.Split(',').Select(s => s.Trim()).ToArray();
services.AddSingleton(Configuration); // Config
services.AddCors(o => o.AddPolicy("default", builder =>
{
builder.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
})); // CORS
TokenVerifier.ControlToken(services, "secretToken");
services.AddSignalR();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDistributedMemoryCache();
services.AddMvc().AddSessionStateTempDataProvider();
services.AddSession(options =>
{
options.Cookie.Name = "MySession";
options.IdleTimeout = TimeSpan.FromDays(1);
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
}
// 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.UseDatabaseErrorPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors("default");
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<MindHub>("/myapp");
});
app.UseMiddleware<ExceptionMiddleware>();
app.UseSession();
app.UseMvc();
}
public void配置服务(IServiceCollection服务)
{
var appConfiguration=新的AppConfigurationManager(配置);
var allowedOrigins=appConfiguration.allowedOrigins.Split(',).Select(s=>s.Trim()).ToArray();
services.AddSingleton(配置);//配置
services.AddCors(o=>o.AddPolicy(“默认”,builder=>
{
带原点的建筑商(允许原点)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));//CORS
ControlToken(服务,“secretToken”);
services.AddSignalR();
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>false;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
AddDistributedMemoryCache();
services.AddMvc().AddSessionStateTempDataProvider();
services.AddSession(选项=>
{
options.Cookie.Name=“MySession”;
options.IdleTimeout=TimeSpan.FromDays(1);
});
services.AddSingleton();
...
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
应用程序UseCors(“默认”);
//app.UseHttpsRedirection();
app.UseAuthentication();
app.usesigner(路由=>
{
routes.MapHub(“/myapp”);
});
app.UseMiddleware();
app.UseSession();
app.UseMvc();
}
请注意,JWT身份验证、CORS和Signal正在工作(可能对你们中的一些人有用)这是我的最终工作示例代码,可能对你们中的一些人有用 注意比顺序更重要
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
string[] allowedOrigins = new string[]; // put your allowed origins here
services.AddSingleton(Configuration); // Config
services.AddCors(o => o.AddPolicy("default", builder =>
{
builder.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
TokenVerifier.ControlToken(services, "secretToken");
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); // HttpContext into ASP.NET Core
// Register your stuff here
}
// 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.UseDatabaseErrorPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors("default");
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<YourHub>("/hubName");
});
app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseMvc();
}
//此方法由运行时调用。使用此方法向容器中添加服务。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
string[]AllowedOriginates=新字符串[];//将允许的原点放在此处
services.AddSingleton(配置);//配置
services.AddCors(o=>o.AddPolicy(“默认”,builder=>
{
带原点的建筑商(允许原点)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
ControlToken(服务,“secretToken”);
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure(x=>
{
x、 ValueLengthLimit=int.MaxValue;
x、 MultipartBodyLengthLimit=long.MaxValue;//如果是multipart
});
services.AddSingleton();//将HttpContext放入ASP.NET核心
//在这里登记你的东西
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
应用程序UseCors(“默认”);
app.UseAuthentication();
app.usesigner(路由=>
{
routes.MapHub(“/hubName”);
});
app.UseMiddleware();
app.UseHttpsRedirection();
app.UseMvc();
}
这是我最后的工作示例代码,可能对你们中的一些人有用
注意比顺序更重要
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
string[] allowedOrigins = new string[]; // put your allowed origins here
services.AddSingleton(Configuration); // Config
services.AddCors(o => o.AddPolicy("default", builder =>
{
builder.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
TokenVerifier.ControlToken(services, "secretToken");
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); // HttpContext into ASP.NET Core
// Register your stuff here
}
// 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.UseDatabaseErrorPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors("default");
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<YourHub>("/hubName");
});
app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseMvc();
}
//此方法由运行时调用。使用此方法向容器中添加服务。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
string[]AllowedOriginates=新字符串[];//将允许的原点放在此处
services.AddSingleton(配置);//配置
services.AddCors(o=>o.AddPolicy(“默认”,builder=>
{
带原点的建筑商(允许原点)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
ControlToken(服务,“secretToken”);
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure(x=>
{
x、 ValueLengthLimit=int.MaxValue;
x、 MultipartBodyLengthLimit=long.MaxValue;//如果是multipart
});
services.AddSingleton();//将HttpContext放入ASP.NET核心
//在这里登记你的东西
}
//T