c# - How to use OnSerializing and OnDeserializing attributes? -


i have tried implement auto encryption , decryption in xml, doesn't work, i.e. data not encrypted. reason? code shown below. i'm using xmlserializer class. thanks

[serializable] public class user {     public string _username;     public string _password;     public string[] _roles;      [xmlignore]     public string username     {         { return _username; }         set { _username = value; }     }      [xmlignore]     public string password     {         { return _password; }         set { _password = value; }     }      [xmlignore]     public string[] roles     {         { return _roles; }         set { _roles = value; }     }      [ondeserializingattribute]     internal void decryptpersonaldata(streamingcontext context)     {         _username = crypto.decrypt(_username);         _password = crypto.decrypt(_password);         (int = 0; < _roles.length; i++)         {             _roles[i] = crypto.decrypt(_roles[i]);         }     }      [onserializingattribute]     internal void encryptpersonaldata(streamingcontext context)     {         _username = crypto.encrypt(_username);         _password = crypto.encrypt(_password);         (int = 0; < _roles.length; i++)         {             _roles[i] = crypto.encrypt(_roles[i]);         }     } } 

ondeserializing isn't used xmlserializer....to perform custom serialization xmlserializer, derive it, , handle ixmldeserializationcallback interface.

here 1 suggested workaround (basically create "twin" class returned encrypted data in it's gets, , did unecryption in sets...you wouldn't use "twin" during serialization task...copy across user user2).

or might able use datacontractserializer instead (but restrictive in doesn't support xml attributes, elements in serialized stream).


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -