3
votes

I am using below code, sometimes I face the exception

System.IO.IOException: operation did not complete successfully because the file contains a virus

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;
}

Exception Detail:

System.IO.IOException: Operation did not complete successfully because the file contains a virus or potentially unwanted software. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 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.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Boolean detectEncodingFromByteOrderMarks) at PageBase.LoadPageStateFromPersistenceMedium() at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

2
Could you please paste the error as text instead of using a screenshot.brass monkey
I added below code.mukesh

2 Answers

1
votes

I don't think Defender is to blame. Unsafe is unsafe. Defender may have been set to guard against the use of this deserializer. Microsoft has labeled LosFormatter as unsafe in 2019, because it allows too much freedom in the type specified,

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

0
votes

Windows Defender Advanced Threat Protection can cause .NET file operations to fail with this exception. This might not be false positive, so first scan your system, but then you can exclude your build directory.

Excluding Build directories can also speed up your build times considerably, but be aware of the risk and make sure you trust the code you're building.