<p>Running into a problem where on certain servers we get an error that the directory name is invalid when using Path.GetTempFileName.  Further investigation shows that it is trying to write a file to c:\Documents and Setting\computername\aspnet\local settings\temp (found by using Path.GetTempPath).  This folder exists so I'm assuming this must be a permissions issue with respect to the asp.net account.  </p>

<p>I've been told by some that Path.GetTempFileName should be pointing to C:\Windows\Microsoft.NET\Framework\v2.0.50727\temporaryasp.net files.</p>

<p>I've also been told that this problem may be due to the order in which IIS and .NET where installed on the server.  I've done the typical 'aspnet_regiis -i' and checked security on the folders etc.  At this point I'm stuck.</p>

<p>Can anyone shed some light on this?</p>

<p>**Update:**Turns out that providing 'IUSR_ComputerName' access to the folder does the trick.  Is that the correct procedure?  I don't seem to recall doing that in the past, and obviously, want to follow best practices to maintain security.  This is, after all, part of a file upload process.</p>

[{'answer_id': 55423, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 0, 'selected': False, 'text': "<p>You can use <strong>Path.GetTempPath()</strong> to find out which directory to which it's trying to write.</p>\n"}, {'answer_id': 55425, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>Could be because <a href="http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/3648346f-e4f5-474b-86c7-5a86e85fa1ff.mspx?mfr=true" rel="nofollow noreferrer">IIS_WPG</a> does not have access to a temp folder. If you think it is a permission issue, run a <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" rel="nofollow noreferrer">Procmon</a> on asp.net worker process and check for AccessDenied errors.</p>\n'}, {'answer_id': 60353, 'author': 'Euro Micelli', 'author_id': 2230, 'author_profile': 'https://Stackoverflow.com/users/2230', 'pm_score': 5, 'selected': True, 'text': '<p>This is probably a combination of impersonation and a mismatch of different authentication methods occurring.</p>\n\n<p>There are many pieces; I\'ll try to go over them one by one.</p>\n\n<p><strong>Impersonation</strong> is a technique to "temporarily" switch the user account under which a thread is running. Essentially, the thread briefly gains the same rights and access -- no more, no less -- as the account that is being impersonated. As soon as the thread is done creating the web page, it "reverts" back to the original account and gets ready for the next call. This technique is used to access resources that only the user logged into your web site has access to. Hold onto the concept for a minute.</p>\n\n<p>Now, by default ASP.NET runs a web site under a local account called <strong>ASPNET</strong>. Again, by default, only the ASPNET account and members of the Administrators group can write to that folder. Your temporary folder is under that account\'s purview. This is the second piece of the puzzle.</p>\n\n<p>Impersonation doesn\'t happen on its own. It needs to be turn on intentionally in your web.config.</p>\n\n<pre><code>&lt;identity impersonate="true" /&gt;\n</code></pre>\n\n<p>If the setting is missing or set to false, your code will execute pure and simply under the ASPNET account mentioned above. Given your error message, I\'m positive that you have impersonation=true. There is nothing wrong with that! Impersonation has advantages and disadvantages that go beyond this discussion.</p>\n\n<p>There is one question left: when you use impersonation, <em>which account gets impersonated</em>?</p>\n\n<p>Unless you specify the account in the web.config (<a href="http://msdn.microsoft.com/en-us/library/72wdk8cc.aspx" rel="noreferrer">full syntax of the identity element here</a>), the account impersonated is the one that the IIS handed over to ASP.NET. And that depends on how the user has authenticated (or not) into the site. That is your third and final piece.</p>\n\n<p>The IUSR_ComputerName account is a low-rights account created by IIS. By default, this account is the account under which a web call runs <strong>if the user could not be authenticated</strong>. That is, the user comes in as an "anonymous".</p>\n\n<p>In summary, this is what is happening to you:</p>\n\n<p>Your user is trying to access the web site, and IIS could not authenticate the person for some reason. Because Anonymous access is ON, (or you would not see IUSRComputerName accessing the temp folder), IIS allows the user in anyway, but as a generic user. Your ASP.NET code runs and impersonates this generic IUSR___ComputerName "guest" account; only now the code doesn\'t have access to the things that the ASPNET account had access to, including its own temporary folder.</p>\n\n<p>Granting IUSR_ComputerName WRITE access to the folder makes your symptoms go away.</p>\n\n<p>But that just the symptoms. You need to review <strong>why is the person coming as "Anonymous/Guest"?</strong></p>\n\n<p>There are two likely scenarios:</p>\n\n<p>a) You intended to use IIS for authentication, but the authentication settings in IIS for some of your servers are wrong.</p>\n\n<p>In that case, you need to disable Anonymous access on those servers so that the usual authentication mechanisms take place. Note that you might still need to grant to your users access to that temporary folder, or use another folder instead, one to which your users already have access.</p>\n\n<p>I have worked with this scenario many times, and quite frankly it gives you less headaches to forgo the Temp folder; create a dedicated folder in the server, set the proper permissions, and set its location in web.config. </p>\n\n<p>b) You didn\'t want to authenticate people anyway, or you wanted to use ASP.NET Forms Authentication (which uses IIS\'s Anonymous access to bypass checks in IIS and lets ASP.NET handle the authentication directly)</p>\n\n<p>This case is a bit more complicated.</p>\n\n<p>You should go to IIS and disable all forms of authentication other than "Anonymous Access". Note that you can\'t do that in the developer\'s box, because the debugger needs Integrated Authentication to be enabled. So your debugging box will behave a bit different than the real server; just be aware of that.</p>\n\n<p>Then, you need to decide whether you should turn impersonation OFF, or conversely, to specify the account to impersonate in the web.config. Do the first if your web server doesn\'t need outside resources (like a database). Do the latter if your web site does need to run under an account that has access to a database (or some other outside resource).</p>\n\n<p>You have two more alternatives to specify the account to impersonate. One, you could go to IIS and change the "anonymous" account to be one with access to the resource instead of the one IIS manages for you. The second alternative is to stash the account and password encrypted in the registry. That step is a bit complicated and also goes beyond the scope of this discussion.</p>\n\n<p>Good luck!</p>\n'}, {'answer_id': 14747986, 'author': 'Mike Gledhill', 'author_id': 391605, 'author_profile': 'https://Stackoverflow.com/users/391605', 'pm_score': 1, 'selected': False, 'text': '<p>I was having the same problem with one of my ASP.Net applications.  I was getting <strong>Path.GetTempPath()</strong> but it was throwing an exception of:</p>\n\n<p><em>"Could not write to file "C:\\Windows\\Temp\\somefilename", exception: Access to the path "C:\\Windows\\Temp\\somefilename" is denied."</em></p>\n\n<p>I tried a few suggestions on this page, but nothing helped.</p>\n\n<p>In the end, I went onto the web server (IIS server) and changed permissions on the server\'s "C:\\Windows\\Temp" directory to give the "Everyone" user full read-write permissions.</p>\n\n<p>And then, finally, the exception went away, and my users could download files from the application.  Phew!</p>\n'}, {'answer_id': 45310564, 'author': 'Brian', 'author_id': 203190, 'author_profile': 'https://Stackoverflow.com/users/203190', 'pm_score': 2, 'selected': False, 'text': "<p>I encountered this error while diagnosing a console app that was writing in temp files. In one of my test iterations I purged all the files/directories in temp for a 'clean-slate' run. I resolved this self inflicted issue by logging out and back in again.</p>\n"}]
['https://Stackoverflow.com/questions/55411', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5678/']