Home All Groups Group Topic Archive Search About

WebClient and proxy authentication

Author
15 Sep 2006 1:38 AM
daniel
Hi,

I have a windows application that is using the System.Net.WebClient class to
download files requested by the user from a remote server.  I am having
problems getting proxy authentication to work correctly.

Essentially I need the application to prompt the user for a username and
password if required for the proxy. I would like to use the windows network
authentication to perform this ( i.e the prompt that you would get in IE when
proxy authentication is required).  Is there a way to get the WebClient to
prompt for authentication automatically if required ( I am always using the
default IE proxy).

Simply put:

WebClient client = new WebClient()
client.Proxy.Credentials = CredentialCache.DefaultCredentials;
// the following line will fail when an authenticating proxy
byte[] data = client.DownloadData( "....");

Regards,
Daniel
--
Daniel

Author
15 Sep 2006 7:08 AM
Vadym Stetsyak
Hello, daniel!

d> I have a windows application that is using the System.Net.WebClient
d> class to download files requested by the user from a remote server.  I
d> am having problems getting proxy authentication to work correctly.

d> Essentially I need the application to prompt the user for a username and
d> password if required for the proxy. I would like to use the windows
d> network authentication to perform this ( i.e the prompt that you would
d> get in IE when proxy authentication is required).  Is there a way to get
d> the WebClient to prompt for authentication automatically if required ( I
d> am always using the default IE proxy).

Afaik no, WebClient will only show you the result of operation. So, you
have to provide the prompt for user credentials and analyze error codes
returned by WebClient.

d> Simply put:

d> WebClient client = new WebClient()
d> client.Proxy.Credentials = CredentialCache.DefaultCredentials;
d> // the following line will fail when an authenticating proxy
d> byte[] data = client.DownloadData( "....");

Try to add NetworkCredential, where you will specify username and password.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
15 Sep 2006 8:01 AM
daniel
Hi Vadym,

Here's some background info:  A user is using IE to browse the internet, and
their proxy requires their credentials before allowing their request through
(an authenticating proxy).  So the proxy challenges IE which then prompts the
user for their credentials, also allowing them the option of remembering
their credentials.

This situation is the same in Windows if opening files on file servers, that
is, Windows challenges the user for their credentials, also allowing them the
option of remembering their credentials.

So, if my program uses WebClient to download a file, it will also get the
the un-authenticated error, at which time, the program needs to pop up the
IE/Windows credentials window for the user's credentials.

I can't use the DefaultCredentials, or DefaultNetworkCredentials because
these don't have the correct credentials for the proxy.  The credential cache
*also* doesn't have any valid credentials.

In short, the user is logged in on one domain, and the proxy (or even the
fileserver) they're connecting to is on another domain, and they need to
authenticate themselves to that domain.

Hope that makes sense.

This functionality exists in Wininet with the InternetErrorDlg() function.

Regards
Daniel


Show quote
"Vadym Stetsyak" wrote:

> Hello, daniel!
>
>  d> I have a windows application that is using the System.Net.WebClient
>  d> class to download files requested by the user from a remote server.  I
>  d> am having problems getting proxy authentication to work correctly.
>
>  d> Essentially I need the application to prompt the user for a username and
>  d> password if required for the proxy. I would like to use the windows
>  d> network authentication to perform this ( i.e the prompt that you would
>  d> get in IE when proxy authentication is required).  Is there a way to get
>  d> the WebClient to prompt for authentication automatically if required ( I
>  d> am always using the default IE proxy).
>
> Afaik no, WebClient will only show you the result of operation. So, you
> have to provide the prompt for user credentials and analyze error codes
> returned by WebClient.
>
>  d> Simply put:
>
>  d> WebClient client = new WebClient()
>  d> client.Proxy.Credentials = CredentialCache.DefaultCredentials;
>  d> // the following line will fail when an authenticating proxy
>  d> byte[] data = client.DownloadData( "....");
>
> Try to add NetworkCredential, where you will specify username and password.
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot
Author
15 Sep 2006 9:10 AM
Vadym Stetsyak
Hello, daniel!

[skipped]

d> So, if my program uses WebClient to download a file, it will also get
d> the the un-authenticated error, at which time, the program needs to pop
d> up the IE/Windows credentials window for the user's credentials.

WebClient doesn't have such functionality, you will have to design your own window and
show it to users.

d> I can't use the DefaultCredentials, or DefaultNetworkCredentials because
d> these don't have the correct credentials for the proxy.  The credential
d> cache *also* doesn't have any valid credentials.

d> In short, the user is logged in on one domain, and the proxy (or even
d> the fileserver) they're connecting to is on another domain, and they
d> need to authenticate themselves to that domain.

Create your own CredentialCache and add NetworkCredential object to it.

CredentialCache myCache = new CredentialCache();
NetworkCredential myCred = new NetworkCredential(
    SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);

myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);

WebClient cl = new WebCleint()
cl.Credentials = myCache;

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
18 Sep 2006 8:04 AM
Peter Huang" [MSFT]
Hi Daniel,

I agree with Vadym's suggestion that the Challenge Dialog is pop up by IE
itself.
If you would like a similar feature for your scenario, I think you may try
to build a dialog just as Vadym's suggestion.

Anyway, please try Vadym's suggestion, if that still did not work for you.
Can you elaborate more information? I am happy to be of assistance.
e.g. what concrete error message did you get?
What is the Proxy software? How is it do the authenticaion?

Thanks for your efforts!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

AddThis Social Bookmark Button