public License ConvertToLicense()
        {
            License license = new License();

            license.id               = this.LicenseId;
            license.tenantId         = this.TenantId;
            license.installationKey  = ResourceProtector.GenerateHash(ResourceProtector.GetHardwareId());
            license.installationDate = DateTime.Now;
            license.computerName     = Environment.MachineName;

            return(license);
        }
Example #2
0
        public static RegistrationInfo ReadKey(String licenseKey, IListener listener)
        {
            RegistrationInfo registrationInfo = null;

            try
            {
                // Caso licenseKey esteja vazia estoura ArgumentNullException, caso licenseKey não
                // esteja em Base64 estoura FormatException e assim por diante
                Byte[] decodedKey       = Convert.FromBase64String(licenseKey);
                String serializedObject = Encoding.UTF8.GetString(decodedKey);
                registrationInfo = (RegistrationInfo)ObjectSerializer.DeserializeObject(serializedObject, typeof(RegistrationInfo));
            }
            catch (Exception exception) // O conteudo da licença não é válido
            {
                AddExceptionData(exception, "License Key = " + licenseKey, null);
                listener.NotifyObject(exception);
                return(null);
            }

            // Verifica se o conteúdo da licença está no formato esperado
            if (registrationInfo == null)
            {
                listener.NotifyObject(new Exception("O conteúdo da licença não está no formato esperado."));
                return(null);
            }

            // Verifica se o usuário tentou forjar uma licença falsa
            if (registrationInfo.Hash == null)
            {
                Exception hashException = new Exception("O hash não estava presente na chave.");
                AddExceptionData(hashException, "License Key = " + licenseKey, null);
                listener.NotifyObject(hashException);
                return(null);
            }
            String hashInput = registrationInfo.ServiceUrl + registrationInfo.TenantId + registrationInfo.LicenseId +
                               registrationInfo.Version + registrationInfo.ExpirationDate.ToString("yyyy-MM-ddTHH:mm:ss");
            String hash = ResourceProtector.GenerateHash(hashInput);

            if (registrationInfo.Hash != hash)
            {
                Exception hashException = new Exception("O hash não confere.");
                AddExceptionData(hashException, "License Key = " + licenseKey, "Hash Input = " + hashInput);
                listener.NotifyObject(hashException);
                return(null);
            }

            return(registrationInfo);
        }
Example #3
0
        public static String GenerateKey(String serviceUrl, int tenantId, int licenseId, DateTime expirationDate)
        {
            String licenseKey;

            RegistrationInfo registrationInfo = new RegistrationInfo(serviceUrl, tenantId, licenseId, expirationDate);
            AssemblyName     assemblyName     = Assembly.GetExecutingAssembly().GetName();

            registrationInfo.Version = assemblyName.Version.ToString();
            String hashInput = registrationInfo.ServiceUrl + registrationInfo.TenantId + registrationInfo.LicenseId +
                               registrationInfo.Version + registrationInfo.ExpirationDate.ToString("yyyy-MM-ddTHH:mm:ss");

            registrationInfo.Hash = ResourceProtector.GenerateHash(hashInput);
            Byte[] serializedObject = ObjectSerializer.SerializeObjectToArray(registrationInfo);
            licenseKey = Convert.ToBase64String(serializedObject);

            return(licenseKey);
        }
Example #4
0
        /// <summary>
        /// Obtem os parâmetros de execução a partir do XML
        /// </summary>
        public static NameValueCollection GetTaskParams()
        {
            NameValueCollection taskParams = new NameValueCollection();

            try
            {
                String        baseDir     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.ToString());
                String        xmlLocation = PathFormat.Adjust(baseDir) + "JobRouting.xml";
                XmlTextReader xmlReader   = new XmlTextReader(xmlLocation);
                xmlReader.ReadStartElement("jobrouting");
                taskParams.Add("url", xmlReader.ReadElementString("url"));
                taskParams.Add("tenantId", xmlReader.ReadElementString("tenantid"));
                taskParams.Add("interval", xmlReader.ReadElementString("interval"));
                taskParams.Add("logDirectories", xmlReader.ReadElementString("logdirectories"));
                taskParams.Add("copyLogDir", xmlReader.ReadElementString("copylogdir"));
                taskParams.Add("installationKey", xmlReader.ReadElementString("installationkey"));
                taskParams.Add("xmlHash", xmlReader.ReadElementString("xmlhash"));
                xmlReader.ReadEndElement();
                xmlReader.Close();
            }
            catch
            {
                return(null);
            }

            String installationKey = ResourceProtector.GetHardwareId();

            installationKey = ResourceProtector.GenerateHash(installationKey);

            String xmlHash = taskParams["url"] + taskParams["tenantId"] + taskParams["interval"] + taskParams["logDirectories"];

            xmlHash = ResourceProtector.GenerateHash(xmlHash);

            // Verifica se os parâmetros estão corretos (batem com os configurados durante a instalação)
            if (installationKey != taskParams["installationkey"])
            {
                return(null);
            }
            if (xmlHash != taskParams["xmlhash"])
            {
                return(null);
            }

            return(taskParams);
        }