using System; using System.Reflection; using System.Runtime.Remoting; using System.Threading; namespace Microsoft.Samples.AppUpdater { public class AppKeys { private const string KEYFILENAME = "AppUpdaterKeys.dll"; private AppDomain AD; private byte[][] KeyList; private string[] ExceptionList; private KeyValidator Validator; private string AppUrl; public AppKeys(string appUrl) { AppUrl = appUrl; } public void InitializeKeyCheck() { UnInitializeKeyCheck(); AD = AppDomain.CreateDomain("KeyValidatorDomain"); BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance; ObjectHandle objectHandle = AD.CreateInstance("AppUpdater", "Microsoft.Samples.AppUpdater.KeyValidator", ignoreCase: false, bindingAttr, null, null, null, null, null); object obj = objectHandle.Unwrap(); Validator = (KeyValidator)obj; KeyList = GetKeyList(AppUrl.TrimEnd('/') + "/AppUpdaterKeys.dll"); } public void UnInitializeKeyCheck() { if (AD != null) { AppDomain.Unload(AD); Thread.Sleep(TimeSpan.FromSeconds(2.0)); AD = null; } } public bool ValidateAssembly(string assemblyLocation) { return Validator.Validate(assemblyLocation, KeyList, ExceptionList); } public byte[][] GetKeyList(string keyFileUrl) { byte[][] array = null; try { AssemblyName assemblyName = new AssemblyName(); assemblyName.CodeBase = keyFileUrl; Assembly assembly = AD.Load(assemblyName); if (KeyValidator.CompareKeys(assembly.GetName().GetPublicKey(), new byte[1][] { Assembly.GetEntryAssembly().GetName().GetPublicKey() })) { Type type = assembly.GetType("Microsoft.Samples.AppUpdater.KeyList"); array = (byte[][])type.GetField("Keys").GetValue(null); ExceptionList = (string[])type.GetField("ExceptionList").GetValue(null); } } catch (Exception) { } byte[][] array2 = null; if (array == null) { array2 = new byte[1][] { Assembly.GetEntryAssembly().GetName().GetPublicKey() }; } else { array2 = new byte[array.Length + 1][]; array2[0] = Assembly.GetEntryAssembly().GetName().GetPublicKey(); array.CopyTo(array2, 1); } return array2; } } }