Sharepoint 2010 使用WebService.ApplyWebConfigModifications()方法以编程方式更改web.config文件时出错

Sharepoint 2010 使用WebService.ApplyWebConfigModifications()方法以编程方式更改web.config文件时出错,sharepoint-2010,web-config,fba,Sharepoint 2010,Web Config,Fba,我已经在SharePoint 2010场中创建了FBA站点。该站点同时允许windows和FBA身份验证。我已将服务器管理员(内置管理员的成员)设置为应用程序池标识。同一帐户在Sql server数据库中对站点具有足够的权限 我实现了一个特性。该功能反过来修改服务器场中站点的web.Config。我已将代码放在SPSecurity.RunWithElevatedPrivileges块中 public override void FeatureActivated(SPFeatureReceiver

我已经在SharePoint 2010场中创建了FBA站点。该站点同时允许windows和FBA身份验证。我已将服务器管理员(内置管理员的成员)设置为应用程序池标识。同一帐户在Sql server数据库中对站点具有足够的权限

我实现了一个特性。该功能反过来修改服务器场中站点的web.Config。我已将代码放在SPSecurity.RunWithElevatedPrivileges块中

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
            Guid siteGuid = ((SPWeb)properties.Feature.Parent).Site.ID;

            Guid webGuid = ((SPWeb)properties.Feature.Parent).ID;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(siteGuid))
                {
                    using (SPWeb web = site.OpenWeb(webGuid))
                    {
                        SPWebApplication webApp = web.Site.WebApplication;  
                        SPWebConfigModification myModification = new SPWebConfigModification("add[@name=\"assembly\"]", "/configuration/system.web/compilation/assemblies");
                        myModification.Value = "<add assembly=\"MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e00227b2bfdcg7e\"/>";
                        myModification.Sequence = 0;
                        myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                        myModification.Owner = typeof(<MyWebpartNameGoesHere>).FullName;
                        webApp.WebConfigModifications.Add(myModification);
                        webApp.WebService.ApplyWebConfigModifications();
                        webApp.Update();

                                                        }
                                               }
                                     });
public override void功能已激活(SPFeatureReceiverProperties)
{
Guid siteGuid=((SPWeb)properties.Feature.Parent).Site.ID;
Guid webGuid=((SPWeb)properties.Feature.Parent).ID;
SPSecurity.runWithLevelatedPrivileges(委托()
{
使用(SPSite站点=新SPSite(siteGuid))
{
使用(SPWeb=site.OpenWeb(webGuid))
{
SPWebApplication webApp=web.Site.WebApplication;
SPWebConfigModification myModification=新的SPWebConfigModification(“添加[@name=\“assembly\”],“/configuration/system.web/compilation/assemblies”);
myModification.Value=“”;
myModification.Sequence=0;
myModification.Type=SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Owner=typeof().FullName;
webApp.WebConfigModifications.Add(myModification);
webApp.WebService.ApplyWebConfigModifications();
webApp.Update();
}
}
});
}

对于任何windows用户来说,代码都运行得非常好。但当我以FBA用户的身份进入网站并尝试激活该功能时,我开始出现“拒绝访问”错误

错误: System.Security.SecurityException:访问被拒绝。 在Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()上 在Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications()上 在Microsoft.SharePoint.Administration.SPWebService.ApplyWebConfigModifications()上


能有人帮我解决这个问题吗?

如果你想让用户激活这个代码,你必须认真考虑。 修改web.config是管理员的事,而不是用户的事

请阅读:


尊敬的Anita

+1根据您显示的代码,此功能只能激活一次。它应该隐藏,在Web应用程序级别确定范围,并在服务器上使用PowerShell或STSADM激活。