I'm trying to invoke a webservice from a console application, and I need to provide the client with a System.Net.NetworkCredential
object.
Is it possible to create a NetworkCredential
object for the user that started the application without prompting for username/password?

- 55,237
- 33
- 144
- 193
3 Answers
If the web service being invoked uses windows integrated security, creating a NetworkCredential
from the current WindowsIdentity
should be sufficient to allow the web service to use the current users windows login. However, if the web service uses a different security model, there isn't any way to extract a users password from the current identity ... that in and of itself would be insecure, allowing you, the developer, to steal your users passwords. You will likely need to provide some way for your user to provide their password, and keep it in some secure cache if you don't want them to have to repeatedly provide it.
Edit: To get the credentials for the current identity, use the following:
Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");
-
1How can you create a NetworkCredential from a WindowsIdentity? – Paolo Tedesco Jun 04 '09 at 09:05
-
16Thanks! I just used CredentialCache.DefaultNetworkCredentials. – Paolo Tedesco Jun 04 '09 at 09:44
-
1Does the GetCredential method return credentials from the stored user names and passwords in Windows XP/2003/Vista/2008? I asked a related question here: http://stackoverflow.com/questions/994232/how-to-call-a-web-service-using-stored-credentials If GetCredential retrieves credentials from the store based on the uri & authentication type, I'll have my answer. I will test this and report back... – Jim Harte Jun 15 '09 at 03:04
-
GetCredentials will only return a credential for the currently logged on user, that may be used to authenticate with the given Uri using the specified authentication method. To my knowledge, GetCredential doesn't actually get any usernames or passwords from windows...it just creates a NetworkCredential instance for the current windows user. – jrista Jun 15 '09 at 04:51
-
1I answered my own question over here: http://stackoverflow.com/questions/994232/how-to-call-a-web-service-using-stored-credentials What I found was that if you're using the new WCF service model, you can handle the authentication declaratively in the app.config and there's no need to mess with Credentials in your code. See my answer over at http://stackoverflow.com/questions/994232/how-to-call-a-web-service-using-stored-credentials for the details. – Jim Harte Jun 15 '09 at 15:00
-
Correct, if you use WCF, it makes dealing with security much, much easier. Usually you only need to deal with NetworkCredential when your connecting to something more legacy, or a custom communications framework. – jrista Jun 15 '09 at 15:59
-
I think you have a typo in your answer: should be `new Uri` instead of `new Url`. – Sarah Vessels Sep 18 '09 at 19:27
You can get the user name using System.Security.Principal.WindowsIdentity.GetCurrent() but there is not way to get current user password!

- 15,808
- 23
- 102
- 173

- 98,240
- 88
- 296
- 433
-
8I do hope that there's no way of getting the current user password! :) What I would like to do is to use the 'current credentials' as a default for the web service. – Paolo Tedesco Jun 04 '09 at 09:02
If you are using HttpClient
, you can use it in the following way to authenticate as the current user:
var httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });

- 263
- 5
- 7