//============================================================================= private void CreateButton_Click(object sender, RoutedEventArgs e) { if (m_VM == null) { return; } if (m_VM.License == null) { return; } // Try to get create license via license server. // For test only. if (false) { WebClient webClient = new WebClient(); webClient.QueryString.Add(LicenseServerData.PARAM_USERNAME, m_VM.License.Username); webClient.QueryString.Add(LicenseServerData.PARAM_PASSWORD, m_VM.License.Password); webClient.QueryString.Add(LicenseServerData.PARAM_ETHERNET_ADDRESS, m_VM.License.EthernetAddress); webClient.QueryString.Add(LicenseServerData.PARAM_GUID, m_VM.License.GUID); webClient.QueryString.Add(LicenseServerData.PARAM_PLATFORM_ID, m_VM.License.PlatformID); webClient.QueryString.Add(LicenseServerData.PARAM_WINVER_MAJOR, m_VM.License.WindowsVersionMajor); webClient.QueryString.Add(LicenseServerData.PARAM_WINVER_MINOR, m_VM.License.WindowsVersionMinor); webClient.QueryString.Add(LicenseServerData.PARAM_EXCEL_VERSION, m_VM.License.ExcelVersion); webClient.DownloadFile(LicenseServerData.LICENSE_GENERATOR_SERVER_URI, "D:\\LicenseTest.lic"); } string strPath = System.IO.Path.Combine(m_VM.LicenseFileDirectory.Trim(), m_VM.LicenseFileName.Trim()); string strError; bool bError = !LicenseUtilities.sCreateLicense(m_VM.License, strPath, out strError); if (bError) { m_VM.DoesStatusContainsError = true; m_VM.Status = strError; } else { m_VM.DoesStatusContainsError = false; m_VM.Status = "File successfully created."; } }
static void Main(string[] args) { // WARNING!!! // Application should have administrator rights otherwise "listener.Start();" throws an exception. try { HttpListener listener = new HttpListener(); listener.Prefixes.Add(LicenseServerData.LICENSE_GENERATOR_SERVER_URI); listener.Start(); Console.WriteLine("Waiting for connections..."); long connectionsCount = 0; while (true) { ++connectionsCount; HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; // Display request and parse parameters. Console.WriteLine("\nConnection #{0}\nQuery: {1}", connectionsCount, request.Url); string strUsername = string.Empty; string strPassword = string.Empty; string strEthAddr = string.Empty; string strGUID = string.Empty; string strEndDate = string.Empty; string strPlatformID = string.Empty; string strWinMajor = string.Empty; string strWinMinor = string.Empty; string strExcelVersion = string.Empty; string strPath = string.Empty; foreach (var queryKey in context.Request.QueryString.Keys) { if (queryKey == null) { continue; } string strQueryKey = queryKey.ToString(); if (string.IsNullOrEmpty(strQueryKey)) { continue; } object queryValue = context.Request.QueryString[strQueryKey]; string strQueryValue = string.Empty; if (queryValue != null) { strQueryValue = queryValue.ToString(); } Console.WriteLine("{0} = {1}", strQueryKey, strQueryValue); if (strQueryKey == LicenseServerData.PARAM_USERNAME) { strUsername = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_PASSWORD) { strPassword = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_ETHERNET_ADDRESS) { strEthAddr = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_GUID) { strGUID = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_END_DATE) { strEndDate = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_PLATFORM_ID) { strPlatformID = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_WINVER_MAJOR) { strWinMajor = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_WINVER_MINOR) { strWinMinor = strQueryValue; } else if (strQueryKey == LicenseServerData.PARAM_EXCEL_VERSION) { strExcelVersion = strQueryValue; } } // Try to create license. LicenseData licenseData = new LicenseData(); licenseData.Username = strUsername; licenseData.Password = strPassword; licenseData.EthernetAddress = strEthAddr; licenseData.GUID = strGUID; // if (string.IsNullOrEmpty(strEndDate)) { licenseData.IncludeDate = false; } else { licenseData.IncludeDate = true; try { licenseData.CanRunTill = Convert.ToDateTime(strEndDate); } catch (Exception ex) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusDescription = "Incorrect parameters."; response.OutputStream.Close(); Console.WriteLine("ERROR, cant parse \"end_date\" parameter: " + ex.Message); continue; } } // licenseData.PlatformID = strPlatformID; licenseData.WindowsVersionMajor = strWinMajor; licenseData.WindowsVersionMinor = strWinMinor; licenseData.ExcelVersion = strExcelVersion; // Try to create license file in the assembly directory. string assemblyFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string licenseFilePath = assemblyFolder + "\\RackDrawingAppLicense.lic"; string strError; bool bError = !LicenseUtilities.sCreateLicense(licenseData, licenseFilePath, out strError); // Send license file in response. if (!bError) { using (FileStream fs = File.OpenRead(licenseFilePath)) { string filename = Path.GetFileName(licenseFilePath); //response is HttpListenerContext.Response... response.ContentLength64 = fs.Length; response.SendChunked = false; response.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet; response.AddHeader("Content-disposition", "attachment; filename=" + filename); byte[] buffer = new byte[64 * 1024]; int read; using (BinaryWriter bw = new BinaryWriter(response.OutputStream)) { while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { bw.Write(buffer, 0, read); bw.Flush(); //seems to have no effect } bw.Close(); } response.StatusCode = (int)HttpStatusCode.OK; response.StatusDescription = "OK"; response.OutputStream.Close(); } Console.WriteLine("SUCCESS. License file was sent."); } else { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusDescription = "Incorrect parameters."; response.OutputStream.Close(); Console.WriteLine("ERROR. " + strError); } File.Delete(licenseFilePath); } } catch (Exception ex) { Console.WriteLine("ERROR: " + ex.Message); Console.WriteLine("Press any key..."); Console.ReadKey(); } }
private void Application_Startup(object sender, StartupEventArgs e) { // Apply application theme from TXT file var assembly = Assembly.GetExecutingAssembly(); var resourceName = "RackDrawingApp_LicenseGenerator.Resources.ApplicationTheme.txt"; // using (Stream stream = assembly.GetManifestResourceStream(resourceName)) { ColorTheme appTheme = ColorTheme.ReadFromStream(stream); // apply theme if (appTheme != null) { CurrentTheme.CurrentColorTheme = appTheme; } } // If arguments are empty then display window. // Otherwise try to parse arguments. if (e.Args.Count() == 0) { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); } else { string strUsername = string.Empty; string strPassword = string.Empty; string strEthAddr = string.Empty; string strGUID = string.Empty; string strEndDate = string.Empty; string strPlatformID = string.Empty; string strWinMajor = string.Empty; string strWinMinor = string.Empty; string strExcelVersion = string.Empty; string strPath = string.Empty; // foreach (string strArg in e.Args) { if (string.IsNullOrEmpty(strArg)) { continue; } if (strArg.StartsWith(ARG_USERNAME)) { strUsername = strArg.Replace(ARG_USERNAME, string.Empty); } else if (strArg.StartsWith(ARG_PASSWORD)) { strPassword = strArg.Replace(ARG_PASSWORD, string.Empty); } else if (strArg.StartsWith(ARG_ETHERNET_ADDRESS)) { strEthAddr = strArg.Replace(ARG_ETHERNET_ADDRESS, string.Empty); } else if (strArg.StartsWith(ARG_GUID)) { strGUID = strArg.Replace(ARG_GUID, string.Empty); } else if (strArg.StartsWith(ARG_END_DATE)) { strGUID = strArg.Replace(ARG_END_DATE, string.Empty); } else if (strArg.StartsWith(ARG_PLATFORM_ID)) { strPlatformID = strArg.Replace(ARG_PLATFORM_ID, string.Empty); } else if (strArg.StartsWith(ARG_WINVER_MAJOR)) { strWinMajor = strArg.Replace(ARG_WINVER_MAJOR, string.Empty); } else if (strArg.StartsWith(ARG_WINVER_MINOR)) { strWinMinor = strArg.Replace(ARG_WINVER_MINOR, string.Empty); } else if (strArg.StartsWith(ARG_EXCEL_VERSION)) { strExcelVersion = strArg.Replace(ARG_EXCEL_VERSION, string.Empty); } else if (strArg.StartsWith(ARG_PATH)) { strPath = strArg.Replace(ARG_PATH, string.Empty); } } LicenseData licenseData = new LicenseData(); licenseData.Username = strUsername; licenseData.Password = strPassword; licenseData.EthernetAddress = strEthAddr; licenseData.GUID = strGUID; // if (string.IsNullOrEmpty(strEndDate)) { licenseData.IncludeDate = false; } else { licenseData.IncludeDate = true; try { licenseData.CanRunTill = Convert.ToDateTime(strEndDate); } catch { return; } } // licenseData.PlatformID = strPlatformID; licenseData.WindowsVersionMajor = strWinMajor; licenseData.WindowsVersionMinor = strWinMinor; licenseData.ExcelVersion = strExcelVersion; string strError; LicenseUtilities.sCreateLicense(licenseData, strPath, out strError); return; } }