0

I've migrated .NET Core 3.1 project to .NET 5 and I am facing problem with write and read cookies from file, because is deprecated.

What is correct way to solve the problem? Thank you

public static CookieContainer ReadCookiesFromDisk(string path)
    {
        if (File.Exists(path) == false)
        {
            return new CookieContainer();
        }

        using (Stream stream = File.Open(path, FileMode.Open))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            return (CookieContainer)formatter.Deserialize(stream);
        }
    }

    public static void WriteCookiesToDisk(string path, CookieContainer cookieContainer)
    {
        if (cookieContainer == null)
        {
            return;
        }
        using (Stream stream = File.Create(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, cookieContainer);
        }
    }

Warning is "BinaryFormatter.Deserialize(Stream)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information."

Error is "BinaryFormatter serialization and deserialization are disabled within this application".

My plan is refactor the code, not allowed deprecated code. Thank you very much

Adam Gajdečka
  • 305
  • 1
  • 4
  • 13

0 Answers0