在远程处理服务器上发送文件流引发SecurityException

本文关键字:文件 SecurityException 服务器 程处理 处理 | 更新日期: 2023-09-27 18:00:19

我正试图将一个感谢文件上传到远程服务器的FileStream。每次我尝试使用下面的功能,与我的远程处理接口:

public static void addAudioFile(FileStream source)
  {
    string fileName = source.Name.Split('''').Last();
    using (FileStream file = new FileStream(audioFolder + "''" + fileName, FileMode.Create, System.IO.FileAccess.Write))
    {
      byte[] bytes = new byte[source.Length];
      source.Read(bytes, 0, (int)source.Length);
      file.Write(bytes, 0, bytes.Length);
      source.Close();
    }
  }
}

我得到这个错误:

L'exception System.Security.SecurityException n'a pas été gérée
  HResult=-2146233078
  Message=Le type System.Runtime.Remoting.ObjRef et les types qui en dérivent (tel que System.Runtime.Remoting.ObjRef) ne peuvent pas être désérialisés avec ce niveau de sécurité.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       à System.Runtime.Serialization.FormatterServices.CheckTypeSecurity(Type t, TypeFilterLevel securityLevel)
       à System.Runtime.Serialization.Formatters.Binary.ObjectReader.CheckSecurity(ParseRecord pr)
       à System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObject(ParseRecord pr)
       à System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
       à System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
       à System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
       à System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
       à System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       à System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       à System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.UnsafeDeserialize(Stream serializationStream, HeaderHandler handler)
       à System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(String objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel securityLevel)
       à System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
    Exception rethrown at [0]: 
       à System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       à System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       à ProjetZenAutomateClient.RemoteInterfaces.IRemoteZenAutomate.addAudioFile(FileStream source)
       à ProjetZenAutomateClient.iZenAutomate.addAudioFile(FileStream source) dans c:'TFVC-Geoffrey'Zen-Geoffreyg'Zen-Projets'GestionAutomate'Client'ProjetZenAutomateClient'iZenAutomate.cs:ligne 107
       à ProjetZenAutomateWPF.ZenProjetEnvironnement.EventSelectionAddAudio(Object sender, RoutedEventArgs e) dans c:'TFVC-Geoffrey'Zen-Geoffreyg'Zen-Projets'GestionAutomate'Client'ProjetZenAutomateWPF'ZenProjetEnvironnement.cs:ligne 329
       à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       à System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       à System.Windows.Controls.Primitives.ButtonBase.OnClick()
       à System.Windows.Controls.Button.OnClick()
       à Microsoft.Windows.Controls.Ribbon.RibbonButton.OnClick()
       à System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       à System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       à System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       à System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       à System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       à System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       à System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       à System.Windows.Input.InputManager.ProcessStagingArea()
       à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       à System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       à System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       à System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       à System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       à System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       à System.Windows.Threading.Dispatcher.Run()
       à System.Windows.Application.RunDispatcher(Object ignore)
       à System.Windows.Application.RunInternal(Window window)
       à System.Windows.Application.Run(Window window)
       à System.Windows.Application.Run()
       à Zen.App.Main() dans c:'TFVC-Geoffrey'Zen-Geoffreyg'Zen-Common'Zen'obj'Debug'App.g.cs:ligne 0
       à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       à System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我可以用文件流检索文件,但不能上传任何文件。

我的客户端电话:

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".wav";
dlg.Filter = "WAVE Files (*.wav)|*.wav";
Nullable<bool> result;
result = dlg.ShowDialog();
if (result == true)
{
  FileStream fs = File.Open(dlg.FileName, FileMode.Open);
  iAuto.addAudioFile(fs);
}

iAuto.addAudioFile(fs)是我的接口方法的调用

在远程处理服务器上发送文件流引发SecurityException

找到了一个解决方案。

我将文件转换为字节数组:

byte[] byteFile = File.ReadAllBytes(dlg.FileName);

感谢你,我能够在我的服务器端应用程序上发送数据,然后我使用下面的代码将其转换回我的服务器上的文件

File.WriteAllBytes(audioFolder + "''" + fileName, file);