c# - Sharepoint 事件接​​收器安全异常

标签 c# security sharepoint iis gac

我有一个 SharePoint 2010 事件接收器,用于监听 SharePoint 列表上的添加/更新/删除事件。当事件发生时,用 C# 编写的事件接收器需要将这些事件传递给 Java 程序。我的代码贴在下面。我可以很好地部署接收器并且它可以工作。我获得了我需要的所有 SharePoint 事件。现在我的问题是将这些事件传递给 Java。每次尝试打开套接字或将 SharePoint 事件写入文件时,我都会遇到安全异常。例如,查看我在下面发布的 ItemDeleting 方法,每次从 SharePoint 列表中删除项目时都会触发该方法。当我尝试打开 Java 的套接字时,出现 System.Net.DnsPermission 异常。如果我不打开套接字而是尝试写入文件,我会收到 System.Security.Permissions.FileIOPermission 异常。

我环顾了论坛并尝试了一些方法来解决问题。首先,我在 SharePoint 的 web.xml 中设置了 trust level="Full"。然后我设置我的程序集的 DeploymentTarget="GlobalAssemblyCache"。这没有帮助。我还可以做些什么?预先感谢您的回复。

我的事件接收者代码:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Net.Sockets;
using System.IO;

namespace EventReceiverTest
{

    public class EventReceiver1 : SPItemEventReceiver
    {

       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
       }

       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);
       }

       public override void ItemDeleting(SPItemEventProperties properties)
       {
           //can't open a socket connection.Request for the permission of type 'System.Net.DnsPermission,... failed
           TcpClient tc = new TcpClient("192.168.xxx.xxx", xxx);
           Console.WriteLine("Server invoked");
           NetworkStream ns = tc.GetStream();
           StreamWriter sw = new StreamWriter(ns);
           sw.WriteLine("item deleted");
           sw.Flush();
           StreamReader sr = new StreamReader(ns);
           Console.WriteLine(sr.ReadLine());


           //can't write to file.  Request for the permission of type 'System.Security.Permissions.FileIOPermission,...failed
           System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\logs\\test.txt");
           file.WriteLine("item deleted " + properties.ListItem);

           //can't output anything to a console
           Console.Out.WriteLine("deleting an item ");

           base.ItemDeleting(properties);

           //but this part works
           properties.Status = SPEventReceiverStatus.CancelWithError;
           properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
       }
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
  <Properties>
    <Property Key="GloballyAvailable" Value="true" />
  </Properties>
</Feature>

<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/">
  <Assemblies>
    <Assembly Location="EventReceiver3.dll" DeploymentTarget="GlobalAssemblyCache" />
  </Assemblies>
  <FeatureManifests>
    <FeatureManifest Location="EventReceiver3_Feature1\Feature.xml" />
  </FeatureManifests>
</Solution>

最佳答案

您需要 FullTrust 并在“SharePoint:System”帐户下运行访问系统资源(如文件)的代码。

起点 - SPSecurity.RunWithElevatedPrivileges .

参见 How to store temp files in SharePoint 2010 hosted code? .

关于c# - Sharepoint 事件接​​收器安全异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11235345/

相关文章:

security - 临时存储敏感数据

java - JAX-WS 和 SharePoint 用户组 Web 服务

sharepoint - 稍后如何更改共享点托管应用程序中的部署 URL?

c# - Windows Mobile 项目中的 System.Reflection

c# - .net 应用程序图标存储在哪里?

javascript - 是否可以在外部浏览器中注入(inject) CSS?

c# - 在 WPF 应用程序中加密凭据

iphone - 检测我的 iPhone 应用程序中的安全漏洞的流程

c# - 跟踪 ASP.NET 应用程序中的所有方法调用

c# - 有没有办法以编程方式将数据列表从sharepoint下载到excel