.net 使用Online Dynamics CRM 2016中的插件时获取System.Security.Permissions.FileIOPermission错误

.net 使用Online Dynamics CRM 2016中的插件时获取System.Security.Permissions.FileIOPermission错误,.net,security,plugins,crm,dynamics-crm-2016,.net,Security,Plugins,Crm,Dynamics Crm 2016,业务流程错误System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”类型的权限失败。在System.Security.CodeAccessSecurityEngine.CheckObject demand、StackScrawMark和stackMark中,System.S

业务流程错误System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”类型的权限失败。在System.Security.CodeAccessSecurityEngine.CheckObject demand、StackScrawMark和stackMark中,System.Security.CodeAccessPermission.demand中的布尔值isPermSet在System.IO.FileStream.InitString路径中,FileMode模式,FileAccess访问,Int32权限,Boolean UserLights,FileShare共享,Int32 bufferSize,FileOptions选项,Security\u属性secAttrs,字符串msgPath、Boolean bFromProxy、Boolean useLongPath、System.IO.FileStream处的Boolean checkHost..ctorString路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、FileOptions选项、字符串msgPath、Boolean bFromProxy、Boolean useLongPath、System.IO.File处的Boolean checkHost、InternalWriteAllBytes字符串路径、Byte[]字节、,RetrieveAttachments.RetrieveClass.ExecuteServiceProvider服务提供程序处的布尔checkHost失败的操作是:请求失败的第一个权限的类型是:System.Security.Permissions.FileIOPermission失败的程序集的区域是:MyComputer

插件代码如下所示:

        try
          {

            QueryExpression notes = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet("filename", "subject", "annotationid", "documentbody","mimetype") };
            notes.Criteria.AddCondition("annotationid", ConditionOperator.Equal, annotationid);
            EntityCollection NotesRetrieve = service.RetrieveMultiple(notes);
            if (NotesRetrieve != null && NotesRetrieve.Entities.Count > 0)
            {
                foreach (var note in NotesRetrieve.Entities)
                {
                       string fileName =note.GetAttributeValue<string>("filename");
                       string cleanFileName = string.Empty;
                       foreach (var chr in fileName.ToCharArray().ToList())
                       {
                           if (!Path.GetInvalidFileNameChars().Contains(chr)) cleanFileName = cleanFileName + chr;
                       }
                       FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"D:\note");      
                       writePermission.Demand();
                       string filePath = Path.Combine(@"D:\note", cleanFileName);
                       byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
                       System.IO.File.WriteAllBytes(filePath, fileContent);


                 }

             }
           }
           catch (Exception ex)
           {
            throw new InvalidPluginExecutionException(ex.ToString());
           }

我有坏消息要告诉你。当您为CRM Online开发插件时,它将无法访问服务器资源(如您的案例中的文件系统),因此您的代码将永远无法在CRM Online环境中工作。

正如您在另外两个问题中所了解的,你不能用沙盒插件将文件保存到服务器的本地驱动器:是的,我已经在前面的问题中发布了相同的问题,但是我找不到更好的方法来解决它?那么,如果你有什么解决方案可以给我建议的话?你认为你的代码到底能做什么?您希望保存的文件最终会出现在哪里?在我之前的问题中,我没有提到它是在线的还是本地的?因此,我无法获得正确的解决方案。我还调试了我的代码。它在文件名、检索文件名和文件路径上正常工作。它在组合路径上也检索了文件内容。但是,当file.writealBytes创建文件时,它将我抛出catchalas,很伤心..哈哈哈,你能给我建议另一种解决方法吗?你能提供你的场景吗,因为我了解你的代码的作用,但我不知道你的最终目标。好的@Andrii基本上我想创建一个插件按钮,从Note Entity下载所有附件文件。插件不是你的情况,因为插件可以交互使用CRM端点和外部端点-查看本文-。要将文件下载到本地存储驱动器,您必须开发控制台应用程序,而不是插件。