Connecting Tech Pros Worldwide Help | Site Map

Bypass username/password dialog when accessing remote network

thoducng@gmail.com
Guest
 
Posts: n/a
#1: Nov 19 '05
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.

Shawn
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Bypass username/password dialog when accessing remote network


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

<thoducng@gmail.com> wrote in message
news:1129577398.268685.169330@g49g2000cwa.googlegr oups.com...[color=blue]
> 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.
>[/color]


Closed Thread