System.IO.IOException:操作未成功完成,因为文件包含病毒

时间:2018-07-19 12:10:28

标签: asp.net

我正在使用下面的代码,有时会遇到异常

  

System.IO.IOException:操作未成功完成,因为文件包含病毒

string name = Request.Form["__VIEWSTATE_KEY"];
object state = null;

string fileName = string.Format("{0}\\{1}.dat", Environment.GetEnvironmentVariable("TEMP"), name );
StreamReader reader = null;
try
{
    reader = new StreamReader(fileName);
    state = (new LosFormatter()).Deserialize(reader);
}
catch (FileNotFoundException)
{
    state = null;
}

异常详细信息:

  

System.IO.IOException:操作未成功完成,因为该文件包含病毒或可能有害的软件。      在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)      在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)      在System.IO.FileStream..ctor处(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,String msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)      在System.IO.StreamReader..ctor处(字符串路径,编码编码,布尔型detectEncodingFromByteOrderMarks,Int32 bufferSize,布尔值CheckHost)      在System.IO.StreamReader..ctor处(字符串路径,编码编码,布尔型detectEncodingFromByteOrderMarks,Int32 bufferSize)      在System.IO.StreamReader..ctor处(字符串路径,布尔型detectEncodingFromByteOrderMarks)      在PageBase.LoadPageStateFromPersistenceMedium()      在System.Web.UI.Page.LoadAllState()      在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)

2 个答案:

答案 0 :(得分:1)

我不认为应该捍卫防守者。不安全就是不安全。 Defender可能已设置为防止使用此反序列化器。微软在2019年将LosFormatter标记为不安全,因为它允许指定类型的太多自由

https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2305?view=vs-2019

答案 1 :(得分:0)

Windows Defender高级威胁防护可能会导致.NET文件操作失败(出现此异常)。这可能不是误报,因此请先扫描您的系统,然后再exclude your build directory

排除构建目录也可以speed up your build times,但是要注意风险,并确保您信任要构建的代码。

相关问题