简体   繁体   中英

Problem writing to a file from webserver

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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM