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; }
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" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message news:uRqsIGJMFHA.3320@TK2MSFTNGP15.phx.gbl... > 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" <bruce@coding-r-us.com> wrote in message > news:%23nc09GEMFHA.3336@TK2MSFTNGP09.phx.gbl... >> >> 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, 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] "Bruce" <bruce@coding-r-us.com> wrote in message news:%23nc09GEMFHA.3336@TK2MSFTNGP09.phx.gbl... > > 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, 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] "Bruce" <coding-r-us.com@newsgroup.nospam> wrote in message news:%23VKD04JMFHA.3196@TK2MSFTNGP10.phx.gbl... > 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" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message > news:uRqsIGJMFHA.3320@TK2MSFTNGP15.phx.gbl... >> 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" <bruce@coding-r-us.com> wrote in message >> news:%23nc09GEMFHA.3336@TK2MSFTNGP09.phx.gbl... >>> >>> 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; >>> } >>> >> >> > >
Don't see what you're looking for? Try a search.
|