c# - 从 SharePoint 中托管的 WCF 服务访问 SPFarm PropertyBag 时出现 SecurityException

标签 c# .net wcf sharepoint sharepoint-2010

我使用 this tutorial 在 SharePoint 2010 (basicHttpBinding) 中托管了一个 WCF 服务.程序集部署到 GAC 并包含 WCF 服务和计时器作业。两者调用相同的方法。 timerjob 工作成功。

但是当我调用 WCF 服务的方法时,我得到一个异常,它不能在 SPFarm PropertyBag 中写入属性。

System.Security.SecurityException: Access denied.
   at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
   at Microsoft.SharePoint.Administration.SPFarm.Update()
   at MyCompany.MyProduct.Business.Config.SetPropertyValue(IPropertyBag propertyBag, String propertyName, String value)
The Zone of the assembly that failed was:
MyComputer

我尝试使用农场管理员帐户调用该方法并尝试使用 SPSecurity.RunWithElevatedPrivileges,但没有成功。

我在提升权限 block 的内部和外部检查了 WindowsIdentity.GetCurrent(),外部是调用者用户,内部是 WebApplications AppPool 的用户

因此正确模拟了 AppPool 用户,但 SharePoint 2010“disallows modification ... to all objects inheriting from SPPersistedObject in the Microsoft.SharePoint.Administration namespace ... from content web applications

文章说,有一个开关 SPWebService.ContentService.RemoteAdministratorAccessDenied(命名空间 Microsoft.SharePoint.Administration)可以消除这种行为,但我不能依赖管理员使用它来运行我的解决方案.

所以我仍然没有解决方案

最佳答案

我找到了解决该问题的技巧。我不会使用它,因为它真的很脏,但也许有人需要它,所以:

  1. 不要通过普通的 Web 应用程序 URL 调用 WCF 服务。使用中央管理的 URL(例如 http://myserver:9999/_vti_bin/project/myservice.svc)
  2. 运行以提升的权限 (SPSecurity.RunWithElevatedPrivileges) 更改农场属性的部分。
  3. 在提升部分之前(现在它真的很脏)设置 System.Web.HttpContext.Current.Items["FormDigestValidated"] = true;

正如我所说,不是很好,但工作...

关于c# - 从 SharePoint 中托管的 WCF 服务访问 SPFarm PropertyBag 时出现 SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3958548/

相关文章:

c# - 防止 DataContractSerializer 中空成员的序列化

c# - .NET Framework 安装没有管理员权限

C# HttpWebRequest.GetResponse - StatusCode 用法是如何处理非异常响应与 webexception 响应的?

.net - 从 NHibernate 映射生成数据库模式

.net - 在控制台应用程序中使用 .NET BackgroundWorker 类

c# - 使用 net.tcp 绑定(bind)在 IIS 7 上托管 WCF 服务 - 使用服务时出错

C# 重构巨大的 switch 语句以使用 LINQ 进行排序

c# - 使用 Task[] 对象引用时未设置为对象的实例

c# - 在对象列表中查找对象数据重复项

wcf - Micromax 和 Samsung 的 J2ME 中的 Web 服务问题