I am trying to store User Id in Session State like this.
var userInfo = HttpContext.Session.GetString("UserId");
I get an exception on the above line. I have seen the fix for similar problem and most say that I need to call app.UseSession() before app.UseMvc() which I already am. I have no clue how to fix this. I have tried to rearrange the code in Startup.cs but was not able to fix this. Please assist.
InvalidOperationException: Session has not been configured for this application or request
I am storing the user Id like this:
HttpContext.Session.Set("UserId", Encoding.ASCII.GetBytes(user.UserId));
Also, the Current Property is always null so I am not sure if it is storing the UserID correctly. (See Snapshot attached)
I am using ASPNET Core 2.2. Code in the Startup Configure method looks like this
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc();
}
In ConfigureServies method, I have added this
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
});