How to resolve Dynamics CRM Plugin System.Security.Permissions.FileIOPermission error

让人想犯罪 __ 提交于 2019-12-02 23:50:57

问题


Business Process Error System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost) at RetrieveAttachments.RetrieveClass.Execute(IServiceProvider serviceProvider) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.FileIOPermission The Zone of the assembly that failed was: MyComputer

I also added following method in plugin AssemblyInfo.cs file [assembly: System.Security.AllowPartiallyTrustedCallers] but its raise same error.

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 f = new FileIOPermission(FileIOPermissionAccess.Write, @"D:\note");
     string fileLocation = Path.Combine(@"D:\note", cleanFileName);
     byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
     System.IO.File.WriteAllBytes(fileLocation, fileContent);
    }
   }

回答1:


Reading file from a sandbox plugin is prohibited. Try to change isolation mode to none.




回答2:


If your plugin is registered in sandbox then this could be the problem. Try to register it outside of the sandbox. Here you can find information about trust levels



来源:https://stackoverflow.com/questions/39347386/how-to-resolve-dynamics-crm-plugin-system-security-permissions-fileiopermission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!