Beispiel #1
0
        public SPLicense.LicenseFile.SPLicenseFile GetLicense(Type type, object instance, bool allowExceptions)
        {
            try
            {
                TestLicense   test    = new TestLicense();
                SPLicenseFile license = test.Create(type, instance, allowExceptions);
                string        key     = new Guid("7F3A08D4-7308-4142-A1DF-DF705136D0A8").ToString();

                SPAdministrationWebApplication centralWebApp = SPAdministrationWebApplication.Local;
                SPSite lowSite = centralWebApp.Sites[0];

                SPUserToken oSysToken = GetSysToken(lowSite.ID);
                using (SPSite centralSite = new SPSite(lowSite.ID, oSysToken))
                {
                    centralSite.AllowUnsafeUpdates = true;


                    using (SPWeb centralWeb = centralSite.OpenWeb())
                    {
                        bool     availableList = true;
                        SPFile   licenseFile   = null;
                        SPFolder licenseFolder = null;

                        centralWeb.AllowUnsafeUpdates = true;
                        try
                        {
                            SPList            LicenseList    = centralWeb.Lists["Hemrika License Files"];
                            SPDocumentLibrary LicenseLibrary = (SPDocumentLibrary)LicenseList;
                            licenseFolder = centralWeb.Folders["Hemrika License Files"];


                            /*
                             * if (!Farmbag.Contains("Hemrika_Encryption_Key"))
                             * {
                             *  Farmbag["Hemrika_Encryption_Key"] = new Guid("7F3A08D4-7308-4142-A1DF-DF705136D0A8").ToString();
                             * }
                             *
                             * encrypt = Farmbag["Hemrika_Encryption_Key"];
                             */

                            SPQuery oQuery = new SPQuery();
                            oQuery.Query = string.Format("<Where><Contains><FieldRef Name=\"FileLeafRef\" /><Value Type=\"File\">" + type.Assembly.FullName + "</Value></Contains></Where><OrderBy><FieldRef Name=\"FileLeafRef\" /></OrderBy>");
                            SPListItemCollection colListItems = LicenseLibrary.GetItems(oQuery);

                            foreach (SPListItem licenseItem in colListItems)
                            {
                                licenseFile = licenseItem.File;
                                break;
                            }
                        }
                        catch (Exception)
                        {
                            availableList = false;
                        }

                        MemoryStream lic = null; //= new MemoryStream();
                        XmlDocument  xml = new XmlDocument();

                        if (licenseFile == null)
                        {
                            lic = new MemoryStream();
                            xml.LoadXml(license.ToXmlString());

                            SPLicenseFile.SaveFile(license, lic, false, String.Empty, false);

                            if (availableList && licenseFolder != null)
                            {
                                licenseFile = licenseFolder.Files.Add(type.Assembly.FullName + ".lic", lic, true);

                                licenseFile.Update();
                                licenseFile.Item.Update();
                            }
                        }

                        if (lic != null)
                        {
                            lic.Close();
                            //lic.Dispose();
                        }

                        if (licenseFile != null)
                        {
                            //byte[] bytes = licenseFile.OpenBinary();
                            //lic = new MemoryStream();
                            //lic.Read(bytes, 0, bytes.Length);

                            license = SPLicenseFile.LoadFile(licenseFile.OpenBinaryStream(), type, false, String.Empty);// true, key);
                        }

                        /*
                         * if (lic != null)
                         * {
                         *  lic.Close();
                         *  //lic.Dispose();
                         * }
                         */
                        centralWeb.AllowUnsafeUpdates = false;
                    }
                    centralSite.AllowUnsafeUpdates = false;
                }

                return(license);
            }
            catch (Exception ex)
            {
                throw new LicenseException(type, instance, ex.Message, ex);
            }
        }
        protected void Btn_Save_Click(object sender, EventArgs e)
        {
            HttpFileCollection hfc = Request.Files;

            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile file = hfc[i];

                Stream openStream;

                if ((openStream = file.InputStream) != null)
                {
                    BufferedStream bufferStream = new BufferedStream(openStream);
                    StreamReader   streamReader = new StreamReader(bufferStream);
                    string         str          = streamReader.ReadToEnd();

                    bufferStream.Position = 0;

                    license = SPLicenseFile.LoadFile(bufferStream, null, false, string.Empty);

                    streamReader.Close();
                    //bufferStream.Close();
                    //openStream.Close();
                }

                List <IConstraint> contraints = license.Constraints;

                foreach (IConstraint contraint in contraints)
                {
                    FarmConstraint farmContraint = contraint as FarmConstraint;
                    if (farmContraint != null)
                    {
                        foreach (string farm in farmContraint.Farms)
                        {
                            tbx_Result.Text += farm + Environment.NewLine;
                        }
                    }

                    DomainConstraint domainContraint = contraint as DomainConstraint;
                    if (domainContraint != null)
                    {
                        foreach (string domain in domainContraint.Domains)
                        {
                            tbx_Result.Text += domain + Environment.NewLine;
                        }
                    }
                }
                //SPLicense.LicenseFile.Constraints.FarmConstraint
                //tbx_Result.Text = license.LicenseKey+ Environment.NewLine;
                tbx_Result.Text += license.Product.ShortName + Environment.NewLine;
                tbx_Result.Text += license.User.Organization + Environment.NewLine;
                tbx_Result.Text += license.Issuer.FullName + Environment.NewLine;

                /*
                 * if (license.Validate())
                 * {
                 *  tbx_Result.Text += license.ToXmlString();
                 * }
                 */
            }
        }