Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinaryFormatter in deep copy is deprecated because it contains security vulnerabilities #6

Open
azrng opened this issue Sep 7, 2023 · 1 comment

Comments

@azrng
Copy link

azrng commented Sep 7, 2023

Whether to consider replacement, official original: Information: https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/binaryformatter-security-guide#binaryformatter-security- vulnerabilities
Controls

@azrng
Copy link
Author

azrng commented Sep 10, 2023

example

public static class CloneExtensions
{
    public static T DeepCopy<T>(this T obj)
        where T : class
    {
        using var ms = new MemoryStream();
        var options = new JsonSerializerOptions
        {
            ReferenceHandler = ReferenceHandler.Preserve,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };
        JsonSerializer.SerializeAsync(ms, obj, options);
        ms.Position = 0;
        return JsonSerializer.Deserialize<T>(ms, options);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant