.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > How to get WebClient to use stored IE login/password to access secure website
 

How to get WebClient to use stored IE login/password to access secure website

Hi.

if I have my login name and password for a website domain stored (in IE)on my machine, can I use these when downloading a file from that website with System.Net.WebClient? Or do I have to enter the login name and password separately for the WebClient?

In VBA, I use CreateObject("Microsoft.XMLHTTP") and, if Iattempt todownload a file fromthe same secure domain, the IE login dialog pops upwith my stored username and password. Can I get the same behaviour using WebClient?

JFK555
For your scenario you should use the CredentialCache class. You can find more information about it at http://msdn.microsoft.com/en-us/library/system.net.credentialcache.aspxand then you'll be able to check if the credentials are stored, otherwise you'll ask for them to the user.




Reymarx Gereda [MSFT]
Reymarx Gereda

You'll have to enter the login name and password separately for WebClient using an instance of NetworkCredential.

WebClient doesn't integrate with IE the same way that XmlHttp does, since XmlHttp uses COM and communicates directly with some IE components.

thanks


Reymarx Gereda [MSFT]
Reymarx Gereda
Thanks. Is thereno way to access the username/password in the Windows 'Stored User Names and Passwords'?

If Windows has stored my credentials for thewebsite domainthen could I use something like http://msdn.microsoft.com/en-us/library/system.net.icredentialsbyhost.getcredential.aspxtoget the credentials stored in Windows for that domain and set it as the NetworkCredential for the WebClient? Or is that not possible?

Adding a login screen to my app would be straight forward enough but, invariably, most people don't remember their passwords - they enter it once and tell IE to remember it.
JFK555
For your scenario you should use the CredentialCache class. You can find more information about it at http://msdn.microsoft.com/en-us/library/system.net.credentialcache.aspxand then you'll be able to check if the credentials are stored, otherwise you'll ask for them to the user.




Reymarx Gereda [MSFT]
Reymarx Gereda

Thanks. I have tried but can't figure out how to get it to work. Assuming Windows (or, to be more specific, IE) knows my credentials for http://www.mydomain.com/secure/ as I saved them via IE then would this be expected to work?

        Dim myCredentialCache As New CredentialCache
        Dim myCredential As New NetworkCredential
        Dim url As Uri
        url = New Uri("http://www.mydomain.com/secure/")
        myCredential = myCredentialCache.GetCredential(url, "Basic")
        wc.Credentials = myCredential

I tried various things but I can't get the credentials stored by IE.

If I add a NetworkCredential and physically add my username and password then I can connect OK.

I have found thatusing http://msdn.microsoft.com/en-us/library/aa302353.aspxmeans I can bring up the Credential Management dialog box and store the username and password with Windows just like IE does. So I can make things workby asking the user for their name/password once and then saving it with Windows like IE, and using the stored value when needed again.

But I can't get the credentials I have already entered and saved via IE.

If I look in the Windows Credential Management, I see that the credentials for the domain I want are there but IE has not stored my username, only the password. Maybe it has storted the username elsewhere (registry?) for security.

JFK555
CredentialCache will not be able to show you the credentials stored in IE. CredentialCache is only an in-memory cache and it is per-process.

Also, what do you mean when you say you want to access credentials enterd and saved in IE? Do you mean:

1) credentials entered in forms when logging onto websites?
2) credentials entered when IE shows you a dialog prompting you to enter credentials - this is mainly for HTTP transport auth mechanisms like Basic,Digest,Kerberos,Negotiate,NTLM

If you are talking about #1, then you cant access that. It is stored by IE in a separate cache. You can only clear that cache using Internet options. I am not sure if you can enumerate that cache. Try and search through IE documentation to see if that is possible.

If you are talking about #2, then I would assume that IE is also using CredUI and therefore those credentials should be available through CredUI.

feroze
--
http://ferozedaud.blogspot.com
http://blogs.msdn.com/feroze_daud
Feroze Daud
I need to pass on a http username/password logonas in option 2. As prompted for and saved with CredUI by IE.

I can't accessthe credentials stored by IEusing CredUI - I use 'cmdkey /list' to make sure I am getting the exact (target) name use by IE but, prompting for this via VB.Net,CredUI brings up a blank username and a password with just one strange character.I would imaginea security measure to prevent an application reading stored usernames and passwords.

That is understandable but I would have thought it would have been easier for WebClient to simply pass on the details stored by a user via CredUI when accessing a website for which the user has stored credentials.

I will have to ask for the username and password again, though using CredUI for this makesit much easier.
JFK555
Are you sure you are marshaling correctly from managed code to unmanaged code? Marshalling is tricky to get working correctly. The fact that you are getting a strange character seems to indicate that something is not quite right in the marshalling.

Can you check out this site:

http://www.microsoft.com/indonesia/msdn/credmgmt.aspx#cred_topic6

here, he has a very nice working application that uses CredUI to manage credentials. It comes with source code. There is also a managed wrapper which I didnt try.

Also, check out http://pinvoke.net to see how your marshalling compares with what is being proposed.

feroze
--
http://ferozedaud.blogspot.com
http://blogs.msdn.com/feroze_daud
Feroze Daud
Thanks. I also found http://blogs.msdn.com/peerchan/pages/487834.aspxand it looks like CredRead is what I'd need. I tried converting that code to VB using an online convertor.

Whilst I couldn't get it to work properly, it failed with a 'Conversion from string "password" to type 'Long' is not valid' error,with "password" revealing the actual password. When I used the TargetName for the credentials I had stored (unencrypted), "password" was, indeed,my password. When I tried CredRead on the IE TargetName I got characters such as 䲵뚉�in the password field and "WININETC" on the end. So presumably IE encrypts the password it stores.

Anyway, thanks for your help but, even if I could get it work, it lookstoo complicated. Ideally, WebClient would have some way of automatically using any storedlogons.
JFK555

You can use google to search for other answers

Custom Search

More Threads

• NetworkCredentials failed to work after setting network security
• How to retrive a cookie from the hard disk
• NetworkStream.Read() does not always read the full string returned from the server
• Server not responding to new clients .. connecting to only one client
• OleDbCommand.ExecuteReader() Error
• WebClient.UploadFile not working
• Welcome to the forum!
• Hi, can you help me ?Asp netThis is my problem, waiting your answers
• System.Net support on winmo 6.x
• how to open System.Security.Cryptography.RijndaelManaged?