Bypass username/password dialog when accessing remote network

T

thoducng

I am writing some code to access share folder on a remote network.


DirectoryInfo dicInfo = new DirectoryInfo("remoteNetwork\shareFolder");



if (dicInfo.Exists)
{
//application code followed



}


The problem is when I ran this code, it always give me dicInfo.Exists =


false even though the directory did exists. If I go to Start\Run\ and
type in \\remoteNetWork\shareFolder, a dialog will pop up ask me for
username/password. After that if I run the code again and
dicInfo.Exists = true, which means the username/password has been
cached somewhere.


I also try to use WebRequest class but with no success:
System.Net.NetworkCredential myCredentials = new
System.Net.NetworkCredential("","","");
myCredentials.Domain = "";
myCredentials.UserName = "username";
myCredentials.Password = "password";
Uri myUrl=new Uri("file://remoteNetwork/shareFolder/New Text
Document.txt");
WebRequest myWebRequest = WebRequest.Create(myUrl);
Console.WriteLine("\n\nRequest to Url is sent.Waiting for
response...Please wait ...");
CredentialCache wrCache = new CredentialCache();
wrCache.Add(myUrl,"NTLM", myCredentials);
myWebRequest.Credentials = wrCache;
myWebRequest.PreAuthenticate = true;
wp.Credentials = wrCache;
WebResponse myWebResponse = myWebRequest.GetResponse();


The exception is thrown at the last line. It is either access is denied



or bad username or password. I also try to change "NTLM" to "Basic"
with no success. If I provide username, password in a dialog as the
above case, the code then work just fine. It means that the
NetworkCredential doesn't even need to do its work.
It is essential for my application to provide username/password behind
the scene because user is not always there to provide it. Any help/
guidance is appreciated.
 
S

Shawn

Give the ASP.NET account access to the share folder or use Impersonation to
impersonate a user with access to the share (like the username and password
you provide dialog box).

Shawn
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top