How to serialize an object in C # and prevent fake?

I have a C # class as follows:

public class TestObj
{
    private int intval;
    private string stringval;
    private int[] intarray;
    private string[] stringarray;

    //... public properties not shown here
}

I would like to serialize an instance of this class to a string.

Moreover:

I will add this line as a QueryString parameter for the URL. Therefore, I would like to make some efforts to ensure that the string cannot be easily modified.

Also, I would like the serialization method to be efficient, so the line size is minmal.

Any suggestions of certain classes / methods of the .NET Framework that I should use?

+3
source share
2 answers

1) For serialization:

 public String SerializeObject(TestObj object)
 {
        String Serialized = String.Empty;
        MemoryStream memoryStream = new MemoryStream ( );
        XmlSerializer xs = new XmlSerializer(typeof(TestObj));
        XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
        xs.Serialize (xmlTextWriter, object);
        memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
        Serialized = UTF8Encoding.GetString(memoryStream.ToArray());
        return Serialized;
 }

code>

2) To prevent interference:

  • , . "MySecretWord".
  • .
  • (, SHA HMAC ( Remus))

( "MySecretWord" ) , , . . , .

Url/Base64 , . , , .

+4

. HMAC, HMACSHA1. , .

+4

Source: https://habr.com/ru/post/1716044/


All Articles