UnauthorizedAccessException

B

Bruce

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

Nicole Calinoiu

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
 
B

Bruce

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

Nicole Calinoiu said:
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


Bruce said:
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;
}
 
N

Nicole Calinoiu

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



Bruce said:
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

Nicole Calinoiu said:
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


Bruce said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top