1

I have a file on a remote server and I want to read this file. lets say the files location is:

string filePath = @"\\192.168.101.15\c$\program files\xxx\test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);

This code is for sure throwing an error: Logon failure: unknown user name or bad password.

How can I pass my credentials??

if I go start/run and put this path, I need to provide credentials lets say Admin and password 123.

Im using Asp.net, c# 3.5

Any Ideas

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
user290611
  • 21
  • 1
  • 1
  • 4
  • Look at 'Looking for best practice for doing a “Net Use” in C#' - http://stackoverflow.com/questions/8919/looking-for-best-practice-for-doing-a-net-use-in-c – Blair Conrad Mar 10 '10 at 15:06

3 Answers3

2

You have to use impersonation, ie execute your code with a user who has acces to the shared folder instead of asp.net user :

http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx

You have two way : -with code -with configuration

remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
  • I added to my web.config Im having the following error Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: Could not create Windows user token from the credentials specified in the config file. Error from the operating system 'Logon failure: unknown user name or bad password. – user290611 Mar 10 '10 at 15:14
0

Your application will need to run as a user that has access to the UNC path, or else impersonate a user with such permissions, for the file load operation.

Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
0

You'd need to be pre-authenticated on the share before you can access the files. It isn't something you can do just by passing a UNC path.

You might consider executing a net use command via the shell programatically. That's the only way I can find to do this.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202