How to Encrypt and decrypt the state file in ASP.net core or blazor?

Saeed Pooladzadeh 251 Reputation points
2021-10-08T17:05:31.08+00:00

Hello,

How to Encrypt and decrypt the state file in ASP.net core or blazor?

regards,

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,393 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,497 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,414 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,637 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Saeed Pooladzadeh 251 Reputation points
    2021-10-08T18:50:34.227+00:00

    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);
            }
    
        }
    
    
    
    }
    

  2. Bruce (SqlWork.com) 61,416 Reputation points
    2021-10-08T20:20:23.77+00:00

    why are you saving state to a file in a blazor app? why not just a state variable? also are all blazor users the same or is this a single user site?

    also in the sample::

    https://github.com/ramtinak/InstagramApiSharp/blob/master/samples/Examples/Program.cs

    at line 91 it says not to use if .net core

    note: I've never used this library, but you appear to be off track

    0 comments No comments

  3. Saeed Pooladzadeh 251 Reputation points
    2021-10-08T20:48:06.453+00:00

    You mean where should I save it? can you describe more?
    When I try to enter Instagram keep getting this message "loading state from file".
    If you have seen the code, do you think which part is wrong?


  4. Saeed Pooladzadeh 251 Reputation points
    2021-10-09T16:58:38.207+00:00

    why not just a state variable?

    Can you explain this?
    Do you mean in a cookie or session?

    thanks,

    0 comments No comments