title: Using WindowsIdentity to get a list of files/directories in ASP.NET while logged in using Forms Authentication
body: <p><strong>Edit 7:</strong><br>
  I'm guessing this can't be done after all, might need to use some kind of command line application to do this and and parse the output, although I expect this could be very slow when lots of people are accessing the folder and/or the NetBIOS session limit is reached.</p>
  
  <p><strong>Edit 6:</strong><br>
  Using Windows Integrated authentication (with an account that has access to the share and the database), I can get the list of files/directories. However, refresh the page and get an <code>UnauthorizedAccessException</code>. Perhaps a NetBIOS or ActiveDirectory limitation.</p>
  
  <p>5 months and still no solution, other than 'use impersonation' (via P/Invoke it seems, since the built in .NET way seems so flaky), but that will also have the same problem. Hard coding username/password is not an option.</p>
  
  <p>Development (Windows 7) box (using IIS or Visual Studio's) built in web server) works fine..</p>
  
  <p><strong>Edit 5:</strong><br>
  From what I have read on the internet and in this question, the only ways for this to work are:</p>
  
  <ul>
  <li>Basic Authentication</li>
  <li>Impersonation</li>
  <li>Have files on same server as Web Application</li>
  </ul>
  
  <p>None of these are an option as either</p>
  
  <ul>
  <li>Users have to login each time - downside: extra barrier to entry, should be a seamless experience</li>
  <li>User name and password hard coded - downside: passwords change, the user coded (asp.net account) in won't have access to the files</li>
  <li>Too many files to store on the web server, not enough disk space</li>
  </ul>
  
  <p>Since there must be Windows Forms applications that can access UNC shared files, I would have thought this would work. That, and the fact when remotely connecting to the web server and using Windows Explorer I can access the files.</p>
  
  <p>Maybe ASP.NET/IIS security prevents it from ever working without impersonation via P/Invoke. Or maybe being blocked by a firewall?</p>
  
  <p><strong>Edit 4:</strong><br />
  One thing that does work is using Basic Authentication - Integrated Windows Authentication and Digest Authentication do not. However, that is not really an option as the password is sent in clear text and the user would get a prompt when they visit the page (not something I want to do).</p>
  
  <p><strong>Edit 3:</strong><br />
  No matter what I try, I can not get a list of files or directories on a UNC path (both a combination of these and individually turned on)</p>
  
  <ol>
  <li>Given 'Everyone' full access to the folder (not just read)</li>
  <li>Given the ASP.NET account 'Act as part of the operating system'</li>
  <li>Given IIS_WPG group 'Act as part of the operating system'</li>
  <li>Used <code>&lt;authentication mode="Windows" /&gt;</code></li>
  <li>Used <code>&lt;identity impersonate="true"/&gt;</code></li>
  </ol>
  
  <p>If I remotely connect (using RDP) to the server with the same account as I use to log into the site, I can access the files fine. It just doesn't work through code.</p>
  
  <p><strong>Edit 2:</strong>
  <br />
  What I have tried is: </p>
  
  <ol>
  <li>Turn on Windows Authentication: files can only be accessed when on same machine (not UNC share), cannot connect to database</li>
  <li>Turn on Forms Authentication, can connect to database but not access files (due to <code>InvalidCastException</code> as Identity is now a <code>GenericIdentity</code>)</li>
  </ol>
  
  <p>I can't use Windows Authentication, as I have no way to add roles. I don't want to prompt for a username / password as that is an extra step and it is not over a secure connection (i.e. SSL). Also I can't give the ASP.NET account access to the files (since I am not a domain administrator).</p>
  
  <p><strong>Edit:</strong>
  <br />
  The path is a UNC path, not on the local PC.</p>
  
  <p>Using forms authentication, when I try to cast the current user identity to a <code>WindowsIdentity</code> in Visual Studio and debugging I get an <code>InvalidCastException</code>. I simply have:</p>
  
  <pre><code>WindowsIdentity id = (WindowsIdentity)HttpContext.Current.User.Identity;
  </code></pre>
  
  <p>Then the error I get is:</p>
  
  <pre><code>Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Security.Principal.WindowsIdentity'
  </code></pre>
  
  <p>I am using impersonation to get a list of files, since the ASP.NET account does not have access to these files. It works when using IIS, but not in Visual Studio:</p>
  
  <pre><code>WindowsIdentity id = (WindowsIdentity)User.Identity;
  using (System.Security.Principal.WindowsImpersonationContext context = id.Impersonate())
  {
   files = Directory.GetFiles("\\server\share");
   context.Undo();
  }
  </code></pre>
  
  <p>Adding <code>&lt;identity impersonate="true" /&gt;</code> to the <code>web.config</code> does not do anything - although I don't want to do that as I am using the ASP.NET account to query a database, which the user does not have access rights to (resulting in <code>Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'</code>).</p>
  
  <p>When I turn on Windows authentication through the web.config I then get <code>UnauthorizedAccessException</code> when trying to get the list of files from a remote share (which I could otherwise access if I remotely connected onto the IIS box as the same user on the client PC).</p>
  
  <p>In addition, the ASP.NET account is an account set up in Active Directory, not local to IIS.</p>