GetTempFileName and UnauthorizedAccessException

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
Ok. I'm about to go out of my mind here. I want to throw my computer out my office window. But I don't have a window in my office. Here's the problem.

System.IO.Path.GetTempFileName() it seems determines the temp directory from System.IO.GetTempPath(), which in turn determines the temp location based on the TMP environment variable.

When you impersonate a user, the environment variables are not updated, which means you can't call GetTempFileName() without getting unauthorized access exception--because it points to some other user's temp area. But I don't let that stop me. I write a wrapper around GetTempFileName, and modify the environment variable on the fly to give me a value that I want. If I point this environment variable to the impersonated user's standard temp area (C:\Documents and settings\username\Local settings\Temp) everything works as expected.

BUT, this temp file needs to be accessible from a few different machines (please don't get caught up in why this needs to be the case--I'm writing some wrapper code around the MS compute cluster API, and I need to specify a standard output file location, and the compute cluster job process could run on any of the valid compute nodes in the cluster), so I set TMP to something like:

\\machinename\temp_root\username

Where machinename is the local machine name, temp_root is a share that I set up, and username is a subdirectory under temp_root where the impersonation user has full control access.

Now, after this, the call to GetTempFileName() is throwing an UnauthorizedAccessException. I have basically made everything WIDE OPEN at this point. The share is wide open, the subfolder is wide open, everything.

I logged in as the user on a different machine on the network, and accessed the share remotely via windows explorer, and created a file in the location where I supposedly do not have access.
 
If you take a look at the help topic for the GetTempFilePath method it mentions:
.NET Framework Security
  • FileIOPermission for writing to the temporary directory. Associated enumeration: FileIOPermissionAccess.Write
I'd suggest reading up about the FileIOPermission class and the FileIOPermissionAccess enumeration.
 
I'd suggest reading up about the FileIOPermission class and the FileIOPermissionAccess enumeration.

Ok, I just read the documentation. I think it tells me in long-winded fashion what I knew already. Namely that I will need to have write access to the temporary directory if I want to use GetTempFileName to write a temp file.

Unfortunately, that doesn't get me closer to understanding why my case is failing. In particular, I believe that I DO have write access to the folder. As my post indicated, I checked this by accessing the share and creating a file in the target directory.

I'm not sure why GetTempFileName is complaining, when the NTFS filesystem is granting me access. That is my real question here.

I am beginning to get a vague idea that GetTempFileName is checking access against the wrong user...namely the user that I was before I did impersonation.
 
I would guess that GetTempFileName will be using credentials for the user under which the current app was started. I certainly don't pretend to have any definitive knowledge in this area though.
 
Back
Top