Beispiel #1
0
        /// <summary>Displays <see cref="NetworkLicenseBrowseForm"/> Initializes licensing objects.</summary>
        private void InitializeLicensingObjects()
        {
            using (NetworkLicenseBrowseForm dialog = new NetworkLicenseBrowseForm())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    LicenseConfiguration.LicenseFilePath = dialog.SelectedPath;
                }
                else
                {
                    Environment.Exit(0);
                }
            }

            //Initialize the license object.
            m_License = SampleLicense.CreateNewLicense(sampleLicensingGui);

            //Now pass the initialized license object to the LicenseingGui component.
            sampleLicensingGui.ApplicationLicense = m_License;

            //Configure the LicensingGui component with the trigger code seed data (only applicable with writable/self-signed licenses).
            sampleLicensingGui.TriggerCodeSeed = LicenseConfiguration.TriggerCodeSeed;
            sampleLicensingGui.RegKey2Seed     = LicenseConfiguration.RegKey2Seed;

            //Wire-up event handlers for the LicensingGui component.  Although this is typically done in the designer, this sample code intentionally
            //initializes to a) make it easier for you to copy-and-paste; and b) prevent the sample functions referenced here from being deleted when
            //the event handlers are changed in the designer.
            sampleLicensingGui.LicenseManagementActionComplete += new EventHandler <LicenseManagementActionCompleteEventArgs>(PostProcessingUpdates);
            sampleLicensingGui.ManualActionLoadRequested       += new EventHandler <ManualActionLoadRequestedEventArgs>(ManualActionLoadRequested);
            sampleLicensingGui.ManualActionSaveRequested       += new EventHandler <ManualActionSaveRequestedEventArgs>(ManualActionSaveRequested);
            sampleLicensingGui.SplashDoWork            += new EventHandler <EventArgs>(InitializeApplicationSettings);
            sampleLicensingGui.SplashWorkCompleteEvent += new EventHandler <EventArgs>(SplashWorkCompleted);
        }
        /// <summary>Revokes the license.</summary>
        /// <remarks><para>This method is called from <see cref="PostProcessingUpdates"/>.</para></remarks>
        /// <param name="lic">The <see cref="License"/> object being revoked.</param>
        /// <param name="licGui">The <see cref="LicensingGui"/> which references the <see cref="License"/> object.</param>
        internal static void RevokeLicense(ref SampleLicense lic, ref LicensingGui licGui)
        {
            //The license has been revoked, so remove it from the system.
            lic.RemoveLicense();

            //Create a new License to clear out any data (like the License ID and Installation ID) from memory.
            lic = SampleLicense.CreateNewLicense(licGui);

            //Now try to load the license file even though it doesn't exist.  This helps ensure the correct error
            //message is displayed in the license status entry if the license management form is opened again.
            lic.LoadFile(LicenseConfiguration.LicenseFilePath);

            //Update the reference to the License object in case the license management form is opened again.
            licGui.ApplicationLicense = lic;
        }
Beispiel #3
0
        /// <summary>The method that performs the work, which is run asynchronously by the splash screen.</summary>
        /// <param name="sender">The sender object.</param>
        /// <param name="e">Event arguments</param>
        /// <remarks><para>This method handles the <see cref="LicensingGui.SplashDoWork"/> event.</para></remarks>
        private void InitializeApplicationSettings(object sender, EventArgs e)
        {
            //Load and initialize the license file.
            m_LastLicenseValidationResult = m_License.InitializeLicense();

            //If License is initialized properly, display status. If we created a fresh evaluation, clear the prior error generated from being unable to load an existing license file.
            if (m_LastLicenseValidationResult && m_License.LastError.ErrorNumber == LicenseError.ERROR_COULD_NOT_LOAD_LICENSE)
            {
                m_License.LastError = new LicenseError(LicenseError.ERROR_NONE);
            }

            //this.criticalFeatureButton.Enabled = UpdateLicenseStatus();

            if (m_Semaphore != null)
            {
                m_Semaphore.Close();
                m_Semaphore = null;
            }

            if (!m_LastLicenseValidationResult && !(m_License.IsWritable) &&
                m_License.ProductOption.OptionType == LicenseProductOption.ProductOptionType.DownloadableLicenseWithTriggerCodeValidation)
            {
                //The license is a downloadable license file, and is not supported since the application license is not a WritableLicense...
                try
                {
                    //Try to delete the downloadable file.
                    File.Delete(LicenseConfiguration.LicenseFilePath);

                    //Re-initialize the settings and the license objects so none of the information from the downloadable license carries over.
                    m_License = SampleLicense.CreateNewLicense(sampleLicensingGui);
                    sampleLicensingGui.ApplicationLicense = m_License;

                    //Try to load and initialize the license file again.
                    m_LastLicenseValidationResult = m_License.InitializeLicense();
                }
                catch (Exception ex)
                {
                    statusLabel.Text = "Error: " + ex.Message;
                }
            }

            m_LastLicenseValidationTime = DateTime.UtcNow;
        }
Beispiel #4
0
        /// <summary>The method that performs the work, which is run asynchronously by the splash screen.</summary>
        /// <remarks><para>This method handles the <see cref="LicensingGui.SplashDoWork"/> event.</para></remarks>
        /// <param name="sender">The sender object.</param>
        /// <param name="e">Event arguments</param>
        private void InitializeApplicationSettings(object sender, EventArgs e)
        {
            //Load and initialize the license file.
            m_LastLicenseValidationResult = m_License.InitializeLicense();

            //If we created a fresh evaluation, clear the prior error generated from being unable to load an existing license file.
            if (m_LastLicenseValidationResult && m_License.LastError.ErrorNumber == LicenseError.ERROR_COULD_NOT_LOAD_LICENSE)
            {
                m_License.LastError = new LicenseError(LicenseError.ERROR_NONE);
            }

            if (!m_LastLicenseValidationResult &&
                !(m_License.IsWritable) &&
                m_License.ProductOption.OptionType == LicenseProductOption.ProductOptionType.DownloadableLicenseWithTriggerCodeValidation)
            {
                //The license is a downloadable license file, and is not supported since the application license is not a WritableLicense...
                string licenseFilePath = (File.Exists(LicenseConfiguration.VolumeLicenseFilePath) ?
                                          LicenseConfiguration.VolumeLicenseFilePath :
                                          LicenseConfiguration.LicenseFilePath);
                try
                {
                    //Try to delete the downloadable file.
                    File.Delete(licenseFilePath);

                    //Re-initialize the settings and the license objects so none of the information from the downloadable license carries over.
                    m_License = SampleLicense.CreateNewLicense(sampleLicensingGui);
                    sampleLicensingGui.ApplicationLicense = m_License;

                    //Try to load and initialize the license file again.
                    m_LastLicenseValidationResult = m_License.InitializeLicense();
                }
                catch (Exception)
                {
                    //We were unable to delete the downloadable license file.
                }
            }

            m_LastLicenseValidationTime = DateTime.UtcNow;

            //TODO: If your application has any of its own initialization logic, you can add it here so it runs while the
            //      splash screen is being displayed.
        }
Beispiel #5
0
        /// <summary>Updates the License's status in the <see cref="LicensingGui"/> and checks if network seats are available.</summary>
        /// <returns>Returns true if the network seats are not depleted, false otherwise</returns>
        private bool UpdateLicenseStatus()
        {
            SampleLicense license = SampleLicense.CreateNewLicense(sampleLicensingGui);

            license.LoadFile(LicenseConfiguration.LicenseFilePath);
            if (license.LicenseID != m_License.LicenseID || license.InstallationID != m_License.InstallationID)
            {
                sampleLicensingGui.ApplicationLicense = license;
                m_License = license;
                if (m_Semaphore != null)
                {
                    m_Semaphore.Close();
                    m_Semaphore = null;
                }
            }

            if (!m_LastLicenseValidationResult || m_License.LastError.ErrorNumber != LicenseError.ERROR_NONE)
            {
                statusLabel.Text = m_License.GenerateNetworkLicenseStatusString(m_LastLicenseValidationResult, m_Semaphore);
                m_License.InitializeNetworkLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui, m_Semaphore);

                //Check seats count and set the status, when No seats available and license re-activation completed with errors(No activation left or Invalid License ID and/or Password).
                if (m_License.LastError.ErrorNumber == LicenseError.ERROR_WEBSERVICE_RETURNED_FAILURE && m_Semaphore == null)
                {
                    m_Semaphore = new NetworkSemaphore(Path.GetDirectoryName(LicenseConfiguration.LicenseFilePath), LicenseConfiguration.NetworkSemaphorePrefix, m_License.LicenseCounter, true, 15, true);
                    if (!m_Semaphore.Open() && m_Semaphore.LastError.ErrorNumber == LicenseError.ERROR_NETWORK_LICENSE_FULL) // try to open a network session
                    {
                        statusLabel.Text = m_License.GenerateLicenseStatusString(m_LastLicenseValidationResult) + "No Seats Available.";
                        m_License.InitializeLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui);
                        sampleLicensingGui.LicenseStatusEntries.Add(new LicenseStatusEntry(LicenseStatusIcon.Information, "Network Seats", "No Seats Available"));
                    }
                    m_Semaphore = null;
                }

                return(false);
            }

            if (!m_License.Validate())
            {
                if (m_Semaphore != null)
                {
                    m_Semaphore.Close(); // close our network session if it is open
                    m_Semaphore = null;
                }
                statusLabel.Text = "The license is invalid or expired";
                m_License.InitializeNetworkLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui, m_Semaphore);
                return(false);
            }
            else
            {
                if (m_Semaphore == null)
                {
                    m_Semaphore          = new NetworkSemaphore(Path.GetDirectoryName(LicenseConfiguration.LicenseFilePath), LicenseConfiguration.NetworkSemaphorePrefix, m_License.LicenseCounter, true, 15, true);
                    m_Semaphore.Invalid += new NetworkSemaphore.InvalidEventHandler(InvalidSemaphoreHandler);

                    if (!m_Semaphore.Open() && m_Semaphore.LastError.ErrorNumber == LicenseError.ERROR_NETWORK_LICENSE_FULL) // try to open a network session
                    {
                        using (NetworkLicenseSearchForm searchDialog = new NetworkLicenseSearchForm(m_Semaphore))
                        {
                            if (searchDialog.ShowDialog() != DialogResult.OK)
                            {
                                statusLabel.Text = m_License.GenerateLicenseStatusString(m_LastLicenseValidationResult) + "No Seats Available.";
                                m_License.InitializeLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui);
                                sampleLicensingGui.LicenseStatusEntries.Add(new LicenseStatusEntry(LicenseStatusIcon.Information, "Network Seats", "No Seats Available"));
                                m_Semaphore = null;
                            }
                        }
                    }
                    else if (m_Semaphore.LastError.ErrorNumber != LicenseError.ERROR_NONE)
                    {
                        statusLabel.Text  = "Status: Unable to establish a network session. " + m_Semaphore.LastError;
                        statusLabel.Text += "\nUsers: N/A";
                        m_Semaphore       = null;
                    }
                    else
                    {
                        statusLabel.Text = m_License.GenerateNetworkLicenseStatusString(m_LastLicenseValidationResult, m_Semaphore);
                        m_License.InitializeNetworkLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui, m_Semaphore);
                    }
                    return(false);
                }
                if (m_Semaphore != null && m_Semaphore.IsValid)
                {
                    statusLabel.Text = m_License.GenerateNetworkLicenseStatusString(m_LastLicenseValidationResult, m_Semaphore);
                    m_License.InitializeNetworkLicenseStatusEntries(m_LastLicenseValidationResult, sampleLicensingGui, m_Semaphore);
                }
                return(true);
            }
        }