Groups | Blog | Home
all groups > asp.net > march 2005 >

asp.net : UnauthorizedAccessException


Bruce
3/23/2005 11:19:48 PM

I need to read a file within a web service, but am having (apparently)
permissions problems. Code snip is posted below.
When I attempt to open a filestream to read a file, I get an
UnauthorizedAccessException exception.

I have granted full permissions on the directory to IUSR_machineName,
ASPNET, Network Service, and Local Service accounts. But granting these did
not fix the problem.

Recommendations? Also, how do I determine definatively what account needs
to be granted permissions for an ASP.NET process (or, in this case, a web
service.)

Thanks,
Bruce

------------------------

[WebMethod]
public byte [] GetBitmap()
{
return GetBitmapPrivate();
}

private byte[] GetBitmapPrivate()
{
FileStream fs = null;
FileInfo inf = new FileInfo(IMAGE_FILE_PATH);
try
{
fs = new FileStream(IMAGE_FILE_PATH, FileMode.Open);
}
catch ( Exception ex )
{
Console.WriteLine( ex.ToString() );
}
byte [] bytes = new byte [inf.Length];
fs.Read(bytes, 0, (int)inf.Length);
fs.Close();
return bytes;
}

Bruce
3/24/2005 10:21:40 AM
Nicole,

Thanks! That worked, and I'm now able to move forward on the project.

However, in the future I'll need to grant write access to the web services
application. Thus, at that point, I'll still need to address the
permissions issue. At least I think that was the problem. So, I'd still
like to answer the question: how do I dermine what account the web service
is using to read/write the files?

Thanks,
-- Bruce

[quoted text, click to view]

Nicole Calinoiu
3/24/2005 11:49:53 AM
Bruce,

Given your use of the FileStream constructor, the file access type is
defaulting to FileAccess.ReadWrite. This may be what is causing your
problem. Even with write access granted, if the file is marked as read-only
or is in use by another process, you may receive UnauthorizedAccessException
on any attempt to open it for writing.

To avoid this problem, simply use a form of the FileStream constructor that
allows you to specify both the FileMode and FileAccess types (e.g.: fs = new
FileStream(IMAGE_FILE_PATH, FileMode.Open, FileAccess.Read);). If you do
this, you should be able to revert to granting only read permissions on the
file to the runtime user context.

HTH,
Nicole


[quoted text, click to view]

Nicole Calinoiu
3/29/2005 8:35:15 AM
Bruce,

WindowsIdentity.GetCurrent() will return the name of the account being used
for accessing Windows resources. For a guide to which account this will be
under various IIS and ASP.NET configurations, see
http://msdn.microsoft.com/library/en-us/dnnetsec/html/SecNetAP05.asp.

HTH,
Nicole



[quoted text, click to view]


AddThis Social Bookmark Button