// // Returns false if it failed to generate run time LicenseKey // either b/c an exception was thrown when creating or opening a // registry subkey, or a regKey could not succesfully write to the // registry. // // Returns boolean newKeyInRegistry // private bool GenerateRunTimeLicenseKeyFromDesignTime(LicenseKey licenseKey, Type type) { Microsoft.Win32.RegistryKey regKey = null; bool newKeyInRegistry = false; // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. The serial number stays the same. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays(licenseKey.EvaluationDays); licenseKey.Hash = licenseKey.CalculateHash(licenseComponent.MasterLicensePassword); // // Write out to the Registry. CreateSubKey returns null if it fails. // If null is returned or an exceptions occurs, catch it and return null. // try { regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(registryCacheName + "\\" + type.GUID.ToString()); if (regKey == null) { Debug.WriteLine("Failed writing to registry."); return(newKeyInRegistry); } regKey.SetValue("License", (string)licenseKey.ToString()); } catch (UnauthorizedAccessException e) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return(newKeyInRegistry); } catch (System.Security.SecurityException e) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return(newKeyInRegistry); } if (regKey != null) { regKey.Close(); } // // Save the new runtime license key string, as it is needed // later by GetKey(). // this.licenseKeyString = licenseKey.ToString(); newKeyInRegistry = true; return(newKeyInRegistry); }
void GenerateLicenseStrings() { int currentSerialNumber = startingSerialNumber; licenseStrings = new string[numberOfLicenseStringsToGenerate]; for (int i = 0; i < numberOfLicenseStringsToGenerate; i++) { LicenseKey licenseKey = new LicenseKey(); if (expires) { licenseKey.Version = 2; licenseKey.Options = LicenseKeyOptions.AbsoluteExpirationDate; licenseKey.ExpirationDate = expirationDate; } if (isEval) { licenseKey.Type = LicenseKeyType.DesignTimeEvaluation; } else { licenseKey.Type = LicenseKeyType.DesignTimeRetail; } licenseKey.EvaluationDays = evalDays; licenseKey.SerialNumber = currentSerialNumber; licenseKey.Hash = licenseKey.CalculateHash(masterKey); licenseStrings[i] = licenseKey.ToString(); currentSerialNumber++; } }
void GenerateLicenseStrings() { int currentSerialNumber = startingSerialNumber; licenseStrings = new string[numberOfLicenseStringsToGenerate]; for(int i=0;i<numberOfLicenseStringsToGenerate;i++) { LicenseKey licenseKey = new LicenseKey(); if ( expires ) { licenseKey.Version = 2; licenseKey.Options = LicenseKeyOptions.AbsoluteExpirationDate; licenseKey.ExpirationDate = expirationDate; } if ( isEval ) { licenseKey.Type = LicenseKeyType.DesignTimeEvaluation; } else { licenseKey.Type = LicenseKeyType.DesignTimeRetail; } licenseKey.EvaluationDays = evalDays; licenseKey.SerialNumber = currentSerialNumber; licenseKey.Hash = licenseKey.CalculateHash( masterKey ); licenseStrings[i] = licenseKey.ToString(); currentSerialNumber++; } }
// // GetKey() is defined by LicFileLicenseProvider. It is called during // GetLicense() processing in order to populate the LicenseKey property // of the License object returned to the caller. // protected override string GetKey(Type type) { string key; LicenseKey licenseKey; if (traceSwitch.TraceVerbose) { Trace.WriteLine(String.Format("GetKey(type={0})", type)); } Trace.Indent(); // // We return whichever license key string we were presented in IsKeyValid(), // unless we had been presented a 'design time evaluation' license. // In the latter case, we generate a new license key that encodes the // evaluation expiration date in it. This license key will subsequenty // be embedded in the executable being built. // key = this.licenseKeyString; // // Myk, what do we do if we catch an exception here? we should // never, because key should be valid. If we return null, will we // still get a license? In that case, we should never throw any // exceptions. // try { licenseKey = new LicenseKey(key); } catch (ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(null); } if (traceSwitch.TraceVerbose) { Trace.WriteLine("GetKey: License type is " + licenseKey.Type); } if (licenseKey.Type == LicenseKeyType.DesignTimeEvaluation) { // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays(licenseKey.EvaluationDays); licenseKey.Hash = licenseKey.CalculateHash(licenseComponent.MasterLicensePassword); key = licenseKey.ToString(); } if (traceSwitch.TraceVerbose) { Trace.WriteLine("Generated license key: " + key); } Trace.Unindent(); // // Return the new license key string to be embedded. // return(key); }
// // Returns false if it failed to generate run time LicenseKey // either b/c an exception was thrown when creating or opening a // registry subkey, or a regKey could not succesfully write to the // registry. // // Returns boolean newKeyInRegistry // private bool GenerateRunTimeLicenseKeyFromDesignTime(LicenseKey licenseKey, Type type) { Microsoft.Win32.RegistryKey regKey = null; bool newKeyInRegistry = false; // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. The serial number stays the same. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); // // Write out to the Registry. CreateSubKey returns null if it fails. // If null is returned or an exceptions occurs, catch it and return null. // try { regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( registryCacheName + "\\" + type.GUID.ToString() ); if ( regKey == null ) { Debug.WriteLine("Failed writing to registry."); return newKeyInRegistry; } regKey.SetValue( "License", (string) licenseKey.ToString() ); } catch( UnauthorizedAccessException e ) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return newKeyInRegistry; } catch( System.Security.SecurityException e) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return newKeyInRegistry; } if( regKey != null ) { regKey.Close(); } // // Save the new runtime license key string, as it is needed // later by GetKey(). // this.licenseKeyString = licenseKey.ToString(); newKeyInRegistry = true; return newKeyInRegistry; }
// // GetKey() is defined by LicFileLicenseProvider. It is called during // GetLicense() processing in order to populate the LicenseKey property // of the License object returned to the caller. // protected override string GetKey(Type type) { string key; LicenseKey licenseKey; if( traceSwitch.TraceVerbose ) { Trace.WriteLine( String.Format( "GetKey(type={0})", type) ); } Trace.Indent(); // // We return whichever license key string we were presented in IsKeyValid(), // unless we had been presented a 'design time evaluation' license. // In the latter case, we generate a new license key that encodes the // evaluation expiration date in it. This license key will subsequenty // be embedded in the executable being built. // key = this.licenseKeyString; // // Myk, what do we do if we catch an exception here? we should // never, because key should be valid. If we return null, will we // still get a license? In that case, we should never throw any // exceptions. // try { licenseKey = new LicenseKey( key ); } catch( ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return null; } if( traceSwitch.TraceVerbose ) { Trace.WriteLine( "GetKey: License type is " + licenseKey.Type); } if( licenseKey.Type == LicenseKeyType.DesignTimeEvaluation ) { // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); key = licenseKey.ToString(); } if( traceSwitch.TraceVerbose ) { Trace.WriteLine("Generated license key: " + key); } Trace.Unindent(); // // Return the new license key string to be embedded. // return key; }