Search code examples
c#wcffile-ioiis-7.5cassini

File.Open not working under Windows 2008R2 IIS with Admin Credentials. Works under VS2010 Cassini


The following code works fine under Cassini, but not at all under IIS. I get file not found, and am unable to get files on a remote share, or locally when I tested C:\test.pdf (to test IIS permissions)

The intent of this application is to make a HTTP proxy that will allow files to be retrieved through a secure URL. The security code has been omitted from this sample. I'm just focusing on file access in this plain-vanilla sample.

I've made sure that the

  1. Application pool (Process Model Identity) is running a Domain Admin account
  2. Website Physical Path Credentials are running under the same admin account
  3. The admin account has both Batch and Run as a Service rights in the local policy.

I access the WCF service using the following URL

http://localhost:1651/services/GetFile.svc/get?swt=\\remoteserver\share\file.pdf

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IGetFile
{
    [OperationContract]
    [WebGet(UriTemplate = "/get?swt={filename}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Get(string filename); 
}


 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GetFile : IGetFile
{
    bool debug = true;

    public Stream Get(string filename )
    {
        //this will cause the file dialog to show the file name instead of "get" 
        WebOperationContext.Current.OutgoingResponse.Headers.Add( "Content-disposition", string.Format("inline; filename={0}", filename));
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octect-stream";

        FileStream fs1= null;

        //WindowsIdentity winId = new WindowsIdentity("aamankow@nfp.com");
        //using (winId.Impersonate())
        {
            try
            {
                fs1 = File.OpenRead(filename);
            }
            catch (FileNotFoundException e)
            {
                if (debug)
                throw;
                else
                return null;
            }
            catch (IOException e)
            {
                if (debug)
                throw;
                else
                // message: Either a required impersonation level was not provided, or the provided impersonation level is invalid.
                return null;
            }
        }

        return fs1;

Solution

  • The code above doesn't work on version 3.5... just 4.0

    Changing the app pool to 4.0 fixed the issue, and allowed me to read from any UNC