I mean session state. I want to encrypt the state.bin.
The app is serverside:
@code{
public static IInstaApi _instaApi;
public static string username { get; set; }
public static string password { get; set; }
public static string message = string.Empty;
public static string ExceptionMessage = string.Empty;
private static async Task StartInstagram()
{
var userSession = new UserSessionData
{
UserName = Enterance.username,
Password = Enterance.password
};
var _instaApi = InstaApiBuilder.CreateBuilder()
.SetUser(userSession)
.UseLogger(new DebugLogger(LogLevel.All))
.Build();
const string stateFile = "state.bin";
try
{
// load session file if exists
if (File.Exists(stateFile))
{
//Console.WriteLine("message = "";");
message = "Loading state from file";
using (var fs = File.OpenRead(stateFile))
{
//_instaApi.LoadStateDataFromStream(fs);
//_instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
////////////////////////////////////////////////////////////////////
//_instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
_instaApi.LoadStateDataFromStream(fs);
}
}
}
catch (Exception e)
{
ExceptionMessage = $" error: {e}";
}
if (!_instaApi.IsUserAuthenticated)
{
// login
message = $"Logging in as {userSession.UserName}";
var logInResult = await _instaApi.LoginAsync();
if (!logInResult.Succeeded)
{
//Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
message = $"Unable to login: {logInResult.Info.Message}";
return;
}
}
// save session in file
var state = _instaApi.GetStateDataAsStream();
using (var fileStream = File.Create(stateFile))
{
state.Seek(0, SeekOrigin.Begin);
state.CopyTo(fileStream);
}
}
}