472,328 Members | 1,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 9513
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...
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...
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...
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...
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...
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.