激活网站功能时,我自动想要设置WebApplication属性。这是代码:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
using (SPSite site = new SPSite(currentWeb.Site.Url))
{
SPWebApplication currentApplication = site.WebApplication;
if (currentApplication.MaxQueryLookupFields < 20)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
currentApplication.MaxQueryLookupFields = 20;
}
catch (System.Security.SecurityException ex)
{
_log.ErrorFormat("no permission");
}
});
}
}
}
即使我是服务器场管理员激活了该功能,也会抛出安全性异常(“拒绝访问”)。在线
currentApplication.MaxQueryLookupFields = 20;
AFAIK SPSecurity.RunWithElevatedPrivileges作为网站管理员运行,不是服务器场管理员。但是如何做到这一点? (没有RunWithElevatedPrivileges我得到了同样的例外。
答案 0 :(得分:0)
您需要在SPSecurity.RunWithElevatedPrivileges中创建新的SPSite,SPWeb和SPWebApplication对象,否则您将使用与当前用户相同的权限运行它们。 E.g。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
using (SPSite site = new SPSite(currentWeb.Site.Url))
{
SPWebApplication currentApplication = site.WebApplication;
if (currentApplication.MaxQueryLookupFields < 20)
{
try
{
currentApplication.MaxQueryLookupFields = 20;
}
catch (System.Security.SecurityException ex)
{
_log.ErrorFormat("no permission");
}
}
}
});
}
答案 1 :(得分:0)
您应该在RWEP中实例化另一个SPSite对象以获取应用程序池标识上下文,因为在RWEP块之外创建的第一个SPSite是使用SPSite当前用户上下文创建的。所以试试这个:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb currentWeb = ContentTypes.ValidateFeatureActivation(properties);
using (SPSite site = new SPSite(currentWeb.Site.Url))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite _site = new SPSite(site.ID))
{
SPWebApplication currentApplication = _site.WebApplication;
if (currentApplication.MaxQueryLookupFields < 20)
{
try
{
currentApplication.MaxQueryLookupFields = 20;
}
catch (System.Security.SecurityException ex)
{
_log.ErrorFormat("no permission");
}
}
}
});
}
}