Reputation: 699
I am using the ASP.NET Core 3.1. I need to set a session inside a action in a controller. I have Added the session in the ConfigureServices() method as:
services.AddSession(options => {
options.IdleTimeout = System.TimeSpan.FromSeconds(3600);
});
Also I have add code to use session in the Configure() method as:
app.UseSession();
In the controller class I am trying to create a session
HttpContext.Session.Set("sessionuser",xxx);
The xxx required is a byte[] unlike the string required in SetString() in .NET CORE 2.2 How to create the byte[] for this? Are there other better options in create sessions ?
Upvotes: 3
Views: 28587
Reputation: 21
You can use it like this. Here I am saving a profile image:
HttpContext.Session.Set("Picture", Encoding.ASCII.GetBytes(PictureBase64));
And for getting this:
if (Context.Session.TryGetValue("Picture", out byte[] pictureByteArr))
{
picture = System.Text.Encoding.ASCII.GetString(pictureByteArr);
}
Upvotes: 0
Reputation: 41
Note:- If you are using Dotnet Core 2.0 or less then first of all install package from Nuget : Microsoft.AspNetCore.Session
Step1: Add Session in ConfigureServices Method
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();
}
Step2: Use Session in Configure Method
app.UseSession();
Step3: add namespace in your controller class
using Microsoft.AspNetCore.Http;
Step4: Now SetString / GetString method would be available with HttpContext.Session
public IActionResult Index()
{
HttpContext.Session.SetString("mysession", "mySessionValue");
}
Upvotes: 4
Reputation: 239380
GetString
/SetString
still exist in .NET Core 3.1. They're extensions (as they always have been) and live in the Microsoft.AspNetCore.Http
namespace. Perhaps you just need to add a using
?
You haven't given a ton of info about your situation here, but it's possible you might be doing this from a class library. In which case, things become a bit trickier. ASP.NET Core 3.X moved to using a framework reference for the bulk of the ASP.NET Core code (instead of a package reference to the Microsoft.AspNetCore.App
metapackage. However, that framework reference can only be utilized when targeting netcoreapp3.0
or above. If you're targeting .NET Standard, you can only reference the old 2.X packages.
If you are only supporting ASP.NET Core 3.X apps, then you should simply change the class library's target to netcoreapp3.1
and add the framework reference. If you need to also support ASP.NET Core 2.X apps, then you'll have to multi-target your class library to both netcoreapp3.1
and netstandard2.0
, and use conditionals in your project file to either add the framework reference or individual 2.X packages depending on the target. Andrew Lock has an excellent guide and more information at: https://andrewlock.net/converting-a-netstandard-2-library-to-netcore-3/
It's also worth mentioning that System.Text.Json
supports serializing directly to/from byte[]
. As such, if you're already using or intend to use generic type extensions for ISession
to support types more complex than simple strings or value types that can be coerced to a string, then you can just rely on that instead:
public static class SessionExtensions
{
public static void Set<T>(this ISession session, string key, T value)
{
session.Set(key, JsonSerializer.SerializeToUtf8Bytes(value));
}
public static T Get<T>(this ISession session, string key)
{
var value = session.Get(key);
return value == null ? default(T) :
JsonSerializer.Deserialize<T>(value);
}
}
Upvotes: 14