예제 #1
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);
        }
예제 #2
0
        // Trata cada tipo de resposta de acordo com o tipo do objeto
        public Object ParseResponse(Type objectType)
        {
            Object parsedValue = null;

            String responseDelimiter = "</response>";
            int    responseLenght    = responseData.IndexOf(responseDelimiter) + responseDelimiter.Length;
            String serializedObject  = responseData.Substring(0, responseLenght);

            // Remove as tags da resposta
            serializedObject = serializedObject.Replace("<response>", "");
            serializedObject = serializedObject.Replace("</response>", "");

            // Se o tipo do objeto for int faz o parse correspondente
            if (objectType == typeof(int))
            {
                parsedValue = int.Parse(serializedObject);
            }

            // Se o tipo do objeto for DateTime faz o parse correspondente
            if (objectType == typeof(DateTime))
            {
                parsedValue = DateTime.Parse(serializedObject);
            }

            // Se o tipo do objeto for Boolean faz o parse correspondente
            if (objectType == typeof(Boolean))
            {
                parsedValue = Boolean.Parse(serializedObject);
            }

            // Se o tipo do objeto for String retorna seu valor sem o parse
            if (objectType == typeof(String))
            {
                parsedValue = serializedObject;
            }

            // Caso não se enquadre em nenhum dos tipos primitivos, trata o tipo como "objeto serializado"
            if (parsedValue == null)
            {
                parsedValue = ObjectSerializer.DeserializeObject(serializedObject, objectType);
            }

            return(parsedValue);
        }