I’m getting a SecurityException, “Access Denied” when trying to make a web.config modification programmatically.
Code closely follows this example:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// create modification
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebConfigModification m = new SPWebConfigModification();
m.Path = "configuration/SharePoint/SafeControls";
m.Name = string.Format(CultureInfo.InvariantCulture, "SafeControl[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Safe='True']", ADSWebPart.GetAssemblyFullName(), ADSWebPart.GetNamespace());
m.Sequence = 0;
m.Owner = SPContext.Current.Web.CurrentUser.Name;
m.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
m.Value = string.Format(CultureInfo.InvariantCulture, "<SafeControl Assembly='{0}' Namespace='{1}' TypeName='*' Safe='True' />", ADSWebPart.GetAssemblyFullName(), ADSWebPart.GetNamespace());
// apply modification
SPWebService service = SPWebService.ContentService;
service.WebConfigModifications.Add(m);
service.Update();
service.ApplyWebConfigModifications();
});
}
(I started with no call to RunWithElevatedPrivileges(), got same exception, then continued enclosing more and more code up to enclosing all body of FeatureActivated().)
Ideas welcome, thanks.
I’m not answering your question as asked, but why are you using a Feature receiver to add SafeControl Entries?
The “Right” way to add SafeControl Entries is to embed these inside <SafeControls> inside the corresponding <Assembly> in manifest.xml of your Solution like this: