Ejemplo n.º 1
0
        private string ConstructFullConnectionString(string connectionString, string login, string password)
        {
            Check.IsNotNullOrEmpty(connectionString, "connectionString");
            Check.IsNotNullOrEmpty(login, "login");
            Check.IsNotNullOrEmpty(password, "password");
            var builder = new StringBuilder();

            builder.Append(connectionString)
            .Append(ConnectionStringSeparator)
            .Append(User)
            .Append(Equal)
            .Append(StringCrypter.Decrypt(login))
            .Append(ConnectionStringSeparator)
            .Append(Password)
            .Append(Equal)
            .Append(StringCrypter.Decrypt(password));
            return(builder.ToString());
        }
Ejemplo n.º 2
0
 public static bool Validate(IWin32Window owner, string applicationType, string applicationName, string fileName)
 {
     try {
         if (!File.Exists(fileName))
         {
             MessageBox.Show(owner, string.Format("File not found."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         AssemblyName inspectedAssemblyName = AssemblyName.GetAssemblyName(fileName);
         if (inspectedAssemblyName == null)
         {
             return(false);
         }
         if (inspectedAssemblyName.GetPublicKey().Length == 0)
         {
             return(false);
         }
         string publicKeyToken = Regex.Matches(Assembly.GetExecutingAssembly().FullName, @"PublicKeyToken=(?<tokenValue>(\w|\d)+)", RegexOptions.IgnoreCase).Cast <Match>().First().Value;
         if (!inspectedAssemblyName.FullName.Contains(publicKeyToken))
         {
             MessageBox.Show(owner, string.Format("License Error: Different PublicKeyToken."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         string licenseFile       = string.Empty;
         string userInfo          = string.Empty;
         string userInfoFile      = Path.ChangeExtension(fileName, "Config.User");
         bool   createNewUserInfo = !File.Exists(userInfoFile);
         if (createNewUserInfo)
         {
             using (LicenseValidatorForm form = new LicenseValidatorForm(applicationType, applicationName, fileName)) {
                 try {
                     form.ShowDialog(owner);
                     userInfo    = form.GetValue();
                     licenseFile = form.GetLicenseFile();
                 } catch {
                     throw;
                 }
             }
         }
         else
         {
             using (StreamReader reader = new StreamReader(userInfoFile)) {
                 userInfo = reader.ReadToEnd();
                 if (string.IsNullOrEmpty(userInfo))
                 {
                     MessageBox.Show(owner, string.Format("Failed to validate the license."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return(false);
                 }
                 try {
                     userInfo = StringCrypter.Decrypt(userInfo);
                 } catch {
                     throw;
                 }
             }
         }
         if (licenseFile.Length == 0)
         {
             licenseFile = Path.ChangeExtension(fileName, "Config.License");
         }
         if (!File.Exists(licenseFile))
         {
             MessageBox.Show(owner, string.Format("Lincense file not found."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(false);
         }
         string license = string.Empty;
         using (StreamReader reader = new StreamReader(licenseFile)) {
             license = reader.ReadToEnd();
             if (string.IsNullOrEmpty(license))
             {
                 MessageBox.Show(owner, string.Format("Fail to validate the license."), string.IsNullOrEmpty(applicationName) ? applicationName : DefaultApplicationInfo.ShortApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return(false);
             }
             try {
                 license = StringCrypter.Decrypt(license);
             } catch {
                 throw;
             }
         }
         try {
             ValidateInternal(userInfo, license);
         } catch {
             throw;
         }
         if (createNewUserInfo)
         {
             using (StreamWriter writer = new StreamWriter(userInfoFile)) {
                 userInfo = StringCrypter.Encrypt(userInfo);
                 writer.Write(userInfo);
                 writer.Flush();
             }
         }
         return(true);
     } catch {
         return(false);
     }
 }