question

SaeedPooladzadeh-4680 avatar image
0 Votes"
SaeedPooladzadeh-4680 asked AgaveJoe edited

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

Hello,

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


regards,

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-core-blazor
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is this a Blazor Server or WASM application? What is a state file? What type of encryption are you using? Are you receiving any errosr? Can you share code the community can easily run that illustrates this problem?

ASP.NET Core Blazor state management


0 Votes 0 ·

The blazor is server-side. I mean session state.
You can see the whole code below.

0 Votes 0 ·
SaeedPooladzadeh-4680 avatar image
0 Votes"
SaeedPooladzadeh-4680 answered AgaveJoe commented

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);
         }
    
     }
    
    
    
 }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I mean session state. I want to encrypt the state.bin

I have no idea what you are asking. Is this an Instagram API question?



0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SaeedPooladzadeh-4680 avatar image
0 Votes"
SaeedPooladzadeh-4680 answered AgaveJoe edited

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?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you have seen the code, do you think which part is wrong?

The GitHub Instagram project is for a single user application like mobile or desktop app not Blazor. A Blazor application has multiple users. As written, every user will read/use the same state.bin file on the server. I doubt that's correct. I agree with Bruce-SqlWork, it seems you are far off track and maybe do not understand how web applications work.

0 Votes 0 ·

I was working in the game development industry. I think you are right that still I can't understand how a web application works.

But there is a lot of sites that their job is related to Instagram!
Then how do they work? do they save the username and password in the session or cookie?

Please let me know your opinion.

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe SaeedPooladzadeh-4680 ·

But there is a lot of sites that their job is related to Instagram! Then how do they work?

I assume the developers read and follow the Instagram developer documentation.

do they save the username and password in the session or cookie?

What username and password are you referring to? Instagram credentials or users of your application? Perhaps try reading the Instagram docs.

Instagram - Get Started


0 Votes 0 ·
SaeedPooladzadeh-4680 avatar image
0 Votes"
SaeedPooladzadeh-4680 answered

why not just a state variable?

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

thanks,



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.