1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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;
- }
- }
- }
|