예제 #1
0
    //-------------------------------------------------------------------------------------------
    private void IssueKey()
    {
        string LicenseKey = GetRequiredParam("LicenseKey");
          string ProductId = GetRequiredParam("ProductId");
          string ProductVersion = GetRequiredParam("ProductVersion");
          string RemoteMachineCode = GetRequiredParam("MachineCode");
          string RemoteMachineName = GetRequiredParam("MachineName");

          using (WeavverEntityContainer data = new WeavverEntityContainer())
          {
               var key = (from y in data.Sales_LicenseKeys
                              where y.Key == LicenseKey
                              select y).FirstOrDefault();

               if (key == null)
               {
                    writer.WriteStartElement("ActivationRequestRejected");
                    WriteAttribute("Reason", "The license key could not be found.");
                    writer.WriteEndElement();
               }
               else
               {
                    data.Detach(key);

                    int activations = key.Activations.Value;

                    // FIND AN EXISTING ACTIVATION FOR THIS MACHINE
                    var existingActivation = (from x in data.Sales_LicenseKeyActivations
                                      where x.LicenseKeyId == key.Id
                                         && x.MachineCode == RemoteMachineCode
                                      select x).FirstOrDefault();

                    if (existingActivation == null)
                    {
                         if (key.Activations.Value < key.MachineCount)
                         {
                              Sales_LicenseKeyActivations activation = new Sales_LicenseKeyActivations();
                              activation.Id = Guid.NewGuid();
                              activation.OrganizationId = SelectedOrganization.Id;
                              activation.LicenseKeyId = key.Id;
                              activation.MachineCode = RemoteMachineCode;
                              activation.LastHeardFrom = DateTime.UtcNow;
                              data.Sales_LicenseKeyActivations.AddObject(activation);

                              activations++;
                         }
                         else
                         {
                              writer.WriteStartElement("ActivationRequestRejected");
                              WriteAttribute("Reason", "Too many activations.");
                              writer.WriteEndElement();
                              return;
                         }
                    }
                    else
                    {
                         existingActivation.LastHeardFrom = DateTime.UtcNow;
                    }
                    data.SaveChanges();

                    if (key != null)
                    {
                         writer.WriteStartElement("MachineLicense");
                         WriteAttribute("LicenseKey", key.Key);
                         WriteAttribute("MachineCode", RemoteMachineCode);
                         WriteAttribute("FullName", key.FullName);
                         WriteAttribute("Organization", key.Organization);
                         WriteAttribute("ConcurrentUsersPerMachine", key.ConcurrentUsersPerMachine.ToString());
                         WriteAttribute("ExpirationDate", key.ExpirationDate.ToString());
                         WriteAttribute("ActivationCount", activations.ToString());
                         if (!String.IsNullOrEmpty(key.ExtraXML))
                              writer.WriteRaw(Environment.NewLine + "     " + key.ExtraXML + Environment.NewLine);
                         writer.WriteEndElement();
                    }
               }
          }
    }