12

Executing code in a web service:

using (File.OpenRead(@"\\server\file.txt"))
{
    // some stuff
}

Causing IO Exception that just says "Invalid Signature"

I can't find anything through google about this error message when using File static methods.

var exists = File.Exists(@"\\server\file.txt"))

Does not throw an exception however, which makes it seem like it can see the file there, but it cannot open it for reading.

I'm really at a loss for what "Invalid Signature" is trying to say in this instance.

Thanks.

UPDATE:

Even using impersonation to open read files from the server, it still throws the IO Exception with "Invalid Signature."

Stack:

<StackTrace>
  <StackFrame Index="6">
    <FileName />
    <Method>Void RenderImages(System.String, System.Nullable`1[System.Int32])</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>88</ILOffset>
  </StackFrame>
  <StackFrame Index="5">
    <FileName />
    <Method>Void .ctor(System.String, Boolean)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>0</ILOffset>
  </StackFrame>
  <StackFrame Index="4">
    <FileName />
    <Method>Void .ctor(System.String, System.Text.Encoding, Boolean, Int32)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>7</ILOffset>
  </StackFrame>
  <StackFrame Index="3">
    <FileName />
    <Method>Void .ctor(System.String, System.Text.Encoding, Boolean, Int32, Boolean)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>113</ILOffset>
  </StackFrame>
  <StackFrame Index="2">
    <FileName />
    <Method>Void .ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions, System.String, Boolean, Boolean, Boolean)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>35</ILOffset>
  </StackFrame>
  <StackFrame Index="1">
    <FileName />
    <Method>Void Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean, Boolean)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>998</ILOffset>
  </StackFrame>
  <StackFrame Index="0">
    <FileName />
    <Method>Void WinIOError(Int32, System.String)</Method>
    <LineNumber>0</LineNumber>
    <ColumnNumber>0</ColumnNumber>
    <ILOffset>571</ILOffset>
  </StackFrame>
</StackTrace>

Environment:

 <Environment>
<DateTime.Now>1/23/2015 8:50:28 AM</DateTime.Now>
<MachineName>xxxxx</MachineName>
<UserDomainName>xxxxx</UserDomainName>
<UserName>xxxx</UserName>
<Executable>w3wp.exe</Executable>
<ExecutableTimestamp>0101.01.0000</ExecutableTimestamp>
<CommandLine>c:\windows\system32\inetsrv\w3wp.exe -ap "xxx.xxxxx.com" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm45ad21f3-41a8-4e9a-a27c-fd0c679b337a -h "C:\inetpub\temp\apppools\xxxx.xxxx.com\xxxx.xxx.com.config" -w "" -m 0 -t 20 -ta 0</CommandLine>
<OSVersion>Microsoft Windows NT 6.3.9600.0</OSVersion>
<WorkingSet>168112128</WorkingSet>
<EnvironmentVersion>4.0.30319.34003</EnvironmentVersion>

using (File.Open(@"\\server\file.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)

and

File.WriteAllText(destinationFile, sb.ToString());

where destinationFile is \\server\sharename\generatedFileName.txt

Also throw the exception, for clarification purposes.

Joel Reid
  • 143
  • 1
  • 11
  • are you sure that is a text file ? can you open it in, say, notepad and see clearly content ? – Tigran Jan 23 '15 at 14:31
  • Is there an InnerException proprerty being populated? – Rob Bell Jan 23 '15 at 14:34
  • Delete all of the text of the path and re type it by hand (I would delete from before the `@` to after the ending `"` to be sure), does it start working? I have had problems in the past where copy/paste copied unprintable characters in to the file path that `File.Open` can't handle. I don't remember if the error I got was was "Invalid Signature" however. – Scott Chamberlain Jan 23 '15 at 14:34
  • 4
    Also please post actual code not just psudocode, `using (File.OpenRead(...` does not make sense, your real code is likely `using (var someVar = File.OpenRead(...`, your simplifications may be hiding the real problem. If possible please edit your question to contain a [MCVE](http://stackoverflow.com/help/mcve). – Scott Chamberlain Jan 23 '15 at 14:38
  • That is the actual code. The file and servers are obviously renamed. I am 100% sure it is a text file. When I tell it to use the C:\ drive to openread a text file, it works fine. I just can't open read or write files to the server. But I can see it because File.Exists does not throw an exception. – Joel Reid Jan 23 '15 at 14:44
  • 1
    Please post the exact exception class name and the call stack. – dymanoid Jan 23 '15 at 14:47
  • Which Identity your web-service Web Application in IIS is running? – VMAtm Jan 23 '15 at 14:52
  • Can you open the file via its unc from the shell on the IIS machine? Is it a Windows Server on the other end? Have you looked in the Event Log? – Alex K. Jan 23 '15 at 14:58
  • Yes, from the IIS machine, we are able to open the file. The server on the other end is a Freebsd machine using Likewise for authentication. – Joel Reid Jan 23 '15 at 15:09
  • I was going to suggest http://support.microsoft.com/kb/2686098 but I would expect it to fail thru the shell as well, nonetheless you match the win2k12 + non MS SMB endpoint – Alex K. Jan 23 '15 at 15:15
  • I know you are renaming folders to protect names but `\\server\file.txt` could never be a valid UNC path, it must be at least `\\server\shareName\file.txt`, are you actually trying to access `\\someServerName\someFileName.txt`? – Scott Chamberlain Jan 23 '15 at 15:18
  • You can use StreamReader and see if get the same exception – prasy Jan 23 '15 at 15:19
  • StreamReader causes the same exception. It is \\server\sharename\file.txt, you are correct. I just left out the \sharename part because I figured that was a given. – Joel Reid Jan 23 '15 at 15:28
  • @VMAtm It's using the ApplicationPoolIdentity but we've tried setting that to a different user with the correct permissions but it still threw the IO Exception with "Invalid Signature" – Joel Reid Jan 23 '15 at 15:47
  • 1
    That's the text of a Windows error, an SSPI security error named NTE_BAD_SIGNATURE. The CryptVerifySignature() function can return it. Something is pretty wonky about that server, I seriously doubt you can get help with it here. – Hans Passant Jan 23 '15 at 16:34
  • Have you looked in the BSD logs? – Alex K. Jan 23 '15 at 17:28
  • One thing we've figured out so far, and I should have specified this, but we were using the \\server.domain.com\share\file.txt which would throw an invalid signature. When we switched it back to \\server\share\file.txt it started throwing invalid user & password. We're getting it straightened out now but thought I would post that for anyone else who runs into that. It was working before with \\server.domain.com but recently we made some hardware & software upgrades and I guess overlooked a couple things. – Joel Reid Jan 23 '15 at 19:11

1 Answers1

1

I've managed to get the same error message. I think the following method helped, but it could just be that I'm forcing the application to reload with every change. In my case, I've been building the file path using a string from a SQL 2000 database.

var folderVar = myDataRow["folderVar"].ToString();
var di = new DirectoryInfo("\\\\domain.website.com\\shares\\" + folderVar + "\\subdir\\");`

The problem stopped after I changed this code to something (horrible) like this:

var folderVar = myDataRow["folderVar"].ToString();
if (folderVar == "ABC")
{
    var di = new DirectoryInfo("\\\\domain.website.com\\shares\\ABC\\subdir\\");`
}
else if (folderVar == "BCD")
{
    var di = new DirectoryInfo("\\\\domain.website.com\\shares\\BCD\\subdir\\");`
}

I don't typically work with string formatting on the byte level. If encoding really is the issue, there's definitely a better solution.

Nathan
  • 1,675
  • 17
  • 25