tags:

views:

82

answers:

2

One requirement of a project I'm working on is to export a file to a network share based on certain criteria. When I run the code in visual studio it works just fine but when I deploy it the actions that would initiate saving the file end in failure. the users see an alert with:

Could not find file '<full filename...>'.

In the web.config I set impersonate to true and the users have permission to write to the network share in question.

Here is the code to write to the file:

using (System.IO.FileStream fs = new System.IO.FileStream(fullpath, System.IO.FileMode.Create))
{
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs))
    {
        sw.WriteLine(RouteMeter.HeaderString);
        foreach (RouteMeter meter in rm)
        {
            sw.WriteLine(meter);
        }
    }
}
A: 

I suspect the better approach here would be to set the app-pool's identity to have access (no impersonation). Also, ensure you are using UNC paths - not network share names ("\\foo\bar", not "f:" etc).

Marc Gravell
+2  A: 

There are many variables that could cause user impersonation to fail, in which case IIS would attempt to perform the action as the anonymous user. Are you verifying that the user is properly authenticated before attempting to write to file?

If impersonation is enabled for an ASP.NET application, that application runs in the context of the identity whose access token IIS passes to ASP.NET. That token can be either an authenticated user token, such as a token for a logged-in Windows user, or the token that IIS provides for anonymous users (typically, the IUSR_MACHINENAME identity).

http://msdn.microsoft.com/en-us/library/xh507fc5.aspx

Mike