Beispiel #1
0
        public static void CheckLicensing(Func <string, string> licenseRequesAction, Action sucessfulActivating)
        {
            Log.Info("START CHECK LICENSE CheckLicensing");
            if (licenseRequesAction == null)
            {
                Log.Error("In CheckLicensing licenseRequesAction == null");
                throw new Exception("Должно передаваться действие для получения лицензии.");
            }
            var exeLocation          = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var liscenseFileFullPath = Path.Combine(Path.GetDirectoryName(exeLocation),
                                                    LicenseFileName);

            Log.Info(string.Format("Check file {0} exist", liscenseFileFullPath));
            if (File.Exists(liscenseFileFullPath))
            {
                try
                {
                    Log.Info(string.Format("Read license file"));
                    var key = File.ReadAllText(liscenseFileFullPath);
                    //MessageBox.Show(key);
                    Log.Info("Check license license file");
                    var resultLicenseVirefy = ClientCoreLicensing.CheckLicenseKeyAsBase64Url(key);
                    Log.Info("Check license license file result = " + resultLicenseVirefy);
                    if (resultLicenseVirefy)
                    {
                        return;
                    }
                }
                catch (Exception exception)
                {
                    Log.Error(exception);
                    throw;
                }
            }
            else
            {
                Log.Info(string.Format("File {0} not exist", liscenseFileFullPath));
            }
            Log.Info("Start request license");
            var recivedKey = licenseRequesAction(SerialNumber);

            if (!string.IsNullOrWhiteSpace(recivedKey))
            {
                bool resultVirefyRecivedKey;
                try
                {
                    Log.Info("Check license license key");
                    resultVirefyRecivedKey = ClientCoreLicensing.CheckLicenseKeyAsBase64Url(recivedKey);
                    Log.Info("Check license key result = " + resultVirefyRecivedKey);
                }
                catch (Exception exc)
                {
                    Log.Error(exc);
                    throw new Exception("Введен не валидный ключ лицензии.");
                }
                if (resultVirefyRecivedKey)
                {
                    try
                    {
                        Log.Info("Start wrinting license key");
                        File.WriteAllText(liscenseFileFullPath, recivedKey);
                        Log.Info("End wrinting license key");
                    }
                    catch (Exception)
                    {
                        throw new Exception("Ошибка активации, требуется активация программы с правами администратора.");
                    }

                    sucessfulActivating();
                    return;
                }
            }
            else
            {
                Log.Error("Введите лицензионнный ключ.");
                throw new Exception("Введите лицензионнный ключ.");
            }
            Log.Error("Введен не валидный ключ лицензии.");
            throw new Exception("Введен не валидный ключ лицензии.");
        }