Пример #1
0
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Definition.Id == PowerWebPartConstants.CentralAdminFeatureId)
            {
                PowerWebPartStore store = PowerWebPartStore.Current;

                if (store == null)
                {
                    store = new PowerWebPartStore(PowerWebPartConstants.StoreName, SPFarm.Local, PowerWebPartConstants.StoreId);

                    CspParameters cspParams = new CspParameters();
                    cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

                    RSACryptoServiceProvider rsaAlg = new RSACryptoServiceProvider(cspParams);
                    rsaAlg.PersistKeyInCsp = false;
                    store.SigningKey       = rsaAlg.ToXmlString(true);

                    store.Update();
                }

                string featurePath = properties.Definition.RootDirectory;

                store.Update(true);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (PowerWebPartHelper.IsPowerUser == false)
            {
                throw new SecurityException("Just for Farm Administrators!");
            }

            store = PowerWebPartStore.Current;

            if (IsPostBack == false)
            {
                txtPowerLibraryUrl.Text             = store.PowerLibraryUrl;
                impersonatePowerLibraryUser.Checked = store.PowerLibraryImpersonate;
            }

            btnSave.Click      += new EventHandler(btnSave_Click);
            btnExportKey.Click += new EventHandler(btnExportKey_Click);
            btnCreateKey.Click += new EventHandler(btnCreateKey_Click);

            if (String.IsNullOrEmpty(txtPowerLibraryUrl.Text) == false)
            {
                ValidateDocumentLibrary();
            }
            store.PowerLibraryUrl = txtPowerLibraryUrl.Text;

            if (uploadKey.HasFile)
            {
                RSACryptoServiceProvider rsaAlg = CreateRsa();
                try
                {
                    string fileAsString = Encoding.UTF8.GetString(uploadKey.FileBytes);
                    rsaAlg.FromXmlString(fileAsString);
                    store.SigningKey = fileAsString;
                }
                catch (Exception ex)
                {
                    throw new Exception("Not a valid key file!");
                }
                finally
                {
                    if (rsaAlg != null)
                    {
                        rsaAlg.Clear();
                    }
                }
            }

            base.OnLoad(e);
        }