473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why can't access a file under mapped network drive from Web Servic

I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear:

13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified network password is not correct.

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at dump_tool.Dump_Load.run()
The source codes relevant are:

Cur_Fs = File.Open( Cur_File, FileMode.Open, FileAccess.Read, FileShare.Read );

Where Cur_File = "o:\jul04\10\fares.fl", o: is a mapped network drive.
Jul 21 '05 #1
6 9650
Because the Framework built in security does not allow this unless the
user that the code is running under (which by default for a Web Service
is ASPNET I believe) has rights to the drive. You need to do one of
three things:

1) Change the rights that the user ASPNET has. Very bad idea. Major
security issues.

2) Change the user that the Web Service is running under to one that
has the correct rights. Better than 1) but still a big security risk.

3) Read up on impersonation and change the code to impersonate a user
that has rights to the drive - for only the amount of time that you are
actively accessing the files. Best option from a security standpoint,
but most work to get working. I have had mixed luck with impersonation
- but I know that the issues that I have are understanding issues, not
implementation issues.

HTH

David

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com:
I write a program accessing files in network drive o:. It is doable as a
standalone application. However, if it is running under windows service, the
following exception will appear:

13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified
network password is not correct.

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at dump_tool.Dump_Load.run()
The source codes relevant are:

Cur_Fs = File.Open( Cur_File, FileMode.Open, FileAccess.Read,
FileShare.Read );

Where Cur_File = "o:\jul04\10\fares.fl", o: is a mapped network drive.


Jul 21 '05 #2
Because the Framework built in security does not allow this unless the
user that the code is running under (which by default for a Web Service
is ASPNET I believe) has rights to the drive. You need to do one of
three things:

1) Change the rights that the user ASPNET has. Very bad idea. Major
security issues.

2) Change the user that the Web Service is running under to one that
has the correct rights. Better than 1) but still a big security risk.

3) Read up on impersonation and change the code to impersonate a user
that has rights to the drive - for only the amount of time that you are
actively accessing the files. Best option from a security standpoint,
but most work to get working. I have had mixed luck with impersonation
- but I know that the issues that I have are understanding issues, not
implementation issues.

HTH

David

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com:
I write a program accessing files in network drive o:. It is doable as a
standalone application. However, if it is running under windows service, the
following exception will appear:

13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified
network password is not correct.

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at dump_tool.Dump_Load.run()
The source codes relevant are:

Cur_Fs = File.Open( Cur_File, FileMode.Open, FileAccess.Read,
FileShare.Read );

Where Cur_File = "o:\jul04\10\fares.fl", o: is a mapped network drive.


Jul 21 '05 #3

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com...
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear:


You can't access mapped drives from a service because mapped drives are
user-specific and become available when a physical login takes place.
Services don't have an associated user session. Use a UNC path instead. In
other words, if mapped drive "o:" is represented by:

'\\computername\neo\trinity"

set the filename argument to

'\\computername\neo\trinity\jul04\10\fares.fl'

FWIW, I'm not really Asian.

Jul 21 '05 #4

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com...
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear:


You can't access mapped drives from a service because mapped drives are
user-specific and become available when a physical login takes place.
Services don't have an associated user session. Use a UNC path instead. In
other words, if mapped drive "o:" is represented by:

'\\computername\neo\trinity"

set the filename argument to

'\\computername\neo\trinity\jul04\10\fares.fl'

FWIW, I'm not really Asian.

Jul 21 '05 #5
David,

I have tried your solution (3) by the following codes:

================================================== ====
public bool set_impersonate()
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;
WindowsIdentity wi;
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

try
{
// Call LogonUser to obtain a handle to an access
token.
bool returnValue = LogonUser
( "xiaodan", "IT1135XD", "xiao2002",
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref tokenHandle );

if( !returnValue )
{
int ret = Marshal.GetLastWin32Error();

DumpLog.WriteEntry( String.Format( "{0}
LogonUser failed with error code : {1}",
DateTime.Now, ret ) );

DumpLog.WriteEntry( String.Format( "{0} Error:
[{1}]", DateTime.Now, ret ) );
return( false );
}

bool retVal = DuplicateToken( tokenHandle,
SecurityImpersonation, ref dupeTokenHandle );
if( !retVal )
{
CloseHandle( tokenHandle );
DumpLog.WriteEntry( String.Format( "Exception
thrown in trying to duplicate token." ) );
return( false );
}

wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "Before
impersonation: {0} User Name = ({1}), Token = {2}",
DateTime.Now, wi.Name, wi.Token ) );

// The token that is passed to the following
// constructor must be a primary token in order
// to use it for impersonation.
WindowsIdentity newId = new WindowsIdentity(
dupeTokenHandle );
WindowsImpersonationContext impersonatedUser =
newId.Impersonate();

wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "After
impersonation: {0} User Name = ({1}), Token =
{2}",
DateTime.Now, wi.Name, wi.Token ) );
return( true );
}
catch( Exception e )
{
Log_sw.WriteLine( "{0} set_impersonate() error:
{1}",
DateTime.Now, e.ToString() );
DumpLog.WriteEntry( String.Format( "{0}
set_impersonate() error: {1}",
DateTime.Now, e.ToString() ) );
return( false );
}
}
================================================== ====

I call the funtion just before the File.Open()
statement to switch the impersonate to my log-on
username/password, shown as follows:

================================================== ====
set_impersonate();

WindowsIdentity wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "Before
File.Open(): {0} User Name = ({1}), Token = {2}",
DateTime.Now, wi.Name, wi.Token ) );

Cur_Fs = File.Open( Cur_File, FileMode.Open,
FileAccess.Read, FileShare.Read );
================================================== ====

The log displays:

================================================== ====
Before File.Open(): 14/07/2004 6:35:15 PM User Name =
(IT1135XD\xiaodan), Token = 828
================================================== ====

Which is exactly my log-on impersonate. However, the
exception is still raised in the File.Open statement
as the follows:

================================================== ====
14/07/2004 6:35:16 PM run() error:
System.IO.IOException: Logon failure: unknown user
name or bad password.

at System.IO.__Error.WinIOError(Int32 errorCode,
String str)
at System.IO.FileStream..ctor(String path, FileMode
mode, FileAccess access, FileShare share, Int32
bufferSize, Boolean useAsync, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode
mode, FileAccess access, FileShare share)
at dump_tool.Dump_Load.run()
================================================== ====

So how to do?

"David Williams" <David Williams>, "VB.NE" wrote:
Because the Framework built in security does not allow this unless the
user that the code is running under (which by default for a Web Service
is ASPNET I believe) has rights to the drive. You need to do one of
three things:

1) Change the rights that the user ASPNET has. Very bad idea. Major
security issues.

2) Change the user that the Web Service is running under to one that
has the correct rights. Better than 1) but still a big security risk.

3) Read up on impersonation and change the code to impersonate a user
that has rights to the drive - for only the amount of time that you are
actively accessing the files. Best option from a security standpoint,
but most work to get working. I have had mixed luck with impersonation
- but I know that the issues that I have are understanding issues, not
implementation issues.

HTH

David

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com:
I write a program accessing files in network drive o:. It is doable as a
standalone application. However, if it is running under windows service, the
following exception will appear:

13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified
network password is not correct.

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at dump_tool.Dump_Load.run()
The source codes relevant are:

Cur_Fs = File.Open( Cur_File, FileMode.Open, FileAccess.Read,
FileShare.Read );

Where Cur_File = "o:\jul04\10\fares.fl", o: is a mapped network drive.


Jul 21 '05 #6
David,

I have tried your solution (3) by the following codes:

================================================== ====
public bool set_impersonate()
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;
WindowsIdentity wi;
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);

tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;

try
{
// Call LogonUser to obtain a handle to an access
token.
bool returnValue = LogonUser
( "xiaodan", "IT1135XD", "xiao2002",
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref tokenHandle );

if( !returnValue )
{
int ret = Marshal.GetLastWin32Error();

DumpLog.WriteEntry( String.Format( "{0}
LogonUser failed with error code : {1}",
DateTime.Now, ret ) );

DumpLog.WriteEntry( String.Format( "{0} Error:
[{1}]", DateTime.Now, ret ) );
return( false );
}

bool retVal = DuplicateToken( tokenHandle,
SecurityImpersonation, ref dupeTokenHandle );
if( !retVal )
{
CloseHandle( tokenHandle );
DumpLog.WriteEntry( String.Format( "Exception
thrown in trying to duplicate token." ) );
return( false );
}

wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "Before
impersonation: {0} User Name = ({1}), Token = {2}",
DateTime.Now, wi.Name, wi.Token ) );

// The token that is passed to the following
// constructor must be a primary token in order
// to use it for impersonation.
WindowsIdentity newId = new WindowsIdentity(
dupeTokenHandle );
WindowsImpersonationContext impersonatedUser =
newId.Impersonate();

wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "After
impersonation: {0} User Name = ({1}), Token =
{2}",
DateTime.Now, wi.Name, wi.Token ) );
return( true );
}
catch( Exception e )
{
Log_sw.WriteLine( "{0} set_impersonate() error:
{1}",
DateTime.Now, e.ToString() );
DumpLog.WriteEntry( String.Format( "{0}
set_impersonate() error: {1}",
DateTime.Now, e.ToString() ) );
return( false );
}
}
================================================== ====

I call the funtion just before the File.Open()
statement to switch the impersonate to my log-on
username/password, shown as follows:

================================================== ====
set_impersonate();

WindowsIdentity wi = WindowsIdentity.GetCurrent();
DumpLog.WriteEntry( String.Format( "Before
File.Open(): {0} User Name = ({1}), Token = {2}",
DateTime.Now, wi.Name, wi.Token ) );

Cur_Fs = File.Open( Cur_File, FileMode.Open,
FileAccess.Read, FileShare.Read );
================================================== ====

The log displays:

================================================== ====
Before File.Open(): 14/07/2004 6:35:15 PM User Name =
(IT1135XD\xiaodan), Token = 828
================================================== ====

Which is exactly my log-on impersonate. However, the
exception is still raised in the File.Open statement
as the follows:

================================================== ====
14/07/2004 6:35:16 PM run() error:
System.IO.IOException: Logon failure: unknown user
name or bad password.

at System.IO.__Error.WinIOError(Int32 errorCode,
String str)
at System.IO.FileStream..ctor(String path, FileMode
mode, FileAccess access, FileShare share, Int32
bufferSize, Boolean useAsync, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode
mode, FileAccess access, FileShare share)
at dump_tool.Dump_Load.run()
================================================== ====

So how to do?

"David Williams" <David Williams>, "VB.NE" wrote:
Because the Framework built in security does not allow this unless the
user that the code is running under (which by default for a Web Service
is ASPNET I believe) has rights to the drive. You need to do one of
three things:

1) Change the rights that the user ASPNET has. Very bad idea. Major
security issues.

2) Change the user that the Web Service is running under to one that
has the correct rights. Better than 1) but still a big security risk.

3) Read up on impersonation and change the code to impersonate a user
that has rights to the drive - for only the amount of time that you are
actively accessing the files. Best option from a security standpoint,
but most work to get working. I have had mixed luck with impersonation
- but I know that the issues that I have are understanding issues, not
implementation issues.

HTH

David

"moonriver" <mo*******@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com:
I write a program accessing files in network drive o:. It is doable as a
standalone application. However, if it is running under windows service, the
following exception will appear:

13/07/2004 10:24:48 AM run() error: System.IO.IOException: The specified
network password is not correct.

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath,
Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at dump_tool.Dump_Load.run()
The source codes relevant are:

Cur_Fs = File.Open( Cur_File, FileMode.Open, FileAccess.Read,
FileShare.Read );

Where Cur_File = "o:\jul04\10\fares.fl", o: is a mapped network drive.


Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Richards | last post by:
I have been told by a local PC club technician that 98SE cannot read NTFS drives in a network. Is this true? TIA, Jim.
5
by: Anthony Boudouvas | last post by:
Hi to all, is there a way to access a mapped network drive from a Windows Service application ? I created a very simple one, using a timer that checks for a file existance on G:\Emails (a...
5
by: Mike McIntyre [MVP] | last post by:
I am working on an issue where and ASP.NET web application on one computer (A) needs to access files on a network mapped drive (B). This application uses as DSN on (A) that maps a proprietory...
2
by: Peter O'Reilly | last post by:
I am experiencing difficulty access a mapped network drive in an ASP.NET application. While using the .Net framework v 1.1 implementation of System.IO.Directory.Exists(), it fails to recognize a...
2
by: Dave Stewart | last post by:
I am attempting to write a vb.net windows service that will automate various file movements on my network. When I attempt to access files on a mapped drive or UNC file share, I get exceptions...
2
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple...
3
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004...
7
by: Ronald S. Cook | last post by:
In my .aspx page, I am trying to read file that is on a different on the company network. When I map a drive to it and call from within my .aspx page, I get this error:
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I am trying to accesss a Mapped Drive from my asp.net code running on IIS 6.0. I am using a FileUpload control and fileUpload.SaveAs() to save the file to the server. I want to save the file to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.