Bypass username/password dialog when accessing remote network

Discussion in 'ASP .Net' started by thoducng@gmail.com, Oct 17, 2005.

  1. Guest

    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.
     
    , Oct 17, 2005
    #1
    1. Advertising

  2. Shawn Guest

    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

    <> wrote in message
    news:...
    > 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, Oct 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M? Carmen G?mez
    Replies:
    1
    Views:
    2,531
  2. zlf
    Replies:
    0
    Views:
    337
  3. Dan Winsor

    username/password dialog prompt

    Dan Winsor, Nov 6, 2009, in forum: Python
    Replies:
    3
    Views:
    1,157
    Dan Winsor
    Nov 9, 2009
  4. Shailesh Patel
    Replies:
    0
    Views:
    466
    Shailesh Patel
    Nov 8, 2006
  5. John Davis
    Replies:
    12
    Views:
    293
    Ken Schaefer
    Mar 5, 2004
Loading...

Share This Page