使用 WebService.ApplyWebConfigModifications() 方法以编程方式更改 web.config 文件会给出错误
我已在 SharePoint 2010 Farm 中创建了 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();
}
}
});
对于任何
Windows 用户来说,代码都可以完美运行。但是,当我以 FBA 用户身份进入该网站并尝试激活该功能时,我开始收到“访问被拒绝”的消息。错误。
错误: System.Security.SecurityException:访问被拒绝。 在 Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate() 在 Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications() 在 Microsoft.SharePoint.Administration.SPWebService.ApplyWebConfigModifications()
任何人都可以帮我解决这个问题吗?
I have created the FBA site in SharePoint 2010 Farm. The site is allowing both the windows and FBA authentication. I have set the Server administrator (Member of Built in Administrator) as Application pool identity. The same account has sufficient permission in Sql server database for the site.
I have implemented one feature. The feature in turn is modifying the web.Config for the sites in farm. I have placed the code inside the SPSecurity.RunWithElevatedPrivileges block.
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();
}
}
});
}
The Code is running perfectly fine for any windows user. But when I enter the site as FBA user and try to activate the feature, I start getting the “Access denied.” error.
Error:
System.Security.SecurityException: Access denied.
at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
at Microsoft.SharePoint.Administration.SPWebApplication.ApplyWebConfigModifications()
at Microsoft.SharePoint.Administration.SPWebService.ApplyWebConfigModifications()
Can anyone help me to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须认真考虑是否希望用户激活此代码。
修改 web.config 是管理员的事情,而不是用户的事情。
请阅读以下内容:http://www.itidea。 nl/index.php/access-denied-when-activating-a-feature/
问候,安妮塔
You seriously have to consider if you want users to activate this code.
Making web.config modifications is an administrators thing and not a users' thing.
Please read this: http://www.itidea.nl/index.php/access-denied-when-activating-a-feature/
Regards, Anita