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

Saeed Pooladzadeh 241 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,

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

4 answers

Sort by: Most helpful
  1. Saeed Pooladzadeh 241 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) 77,686 Reputation points Volunteer Moderator
    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 241 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 241 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.