How do you get credentials (NetworkCredential) of currently logged in user?

Black Light picture Black Light · Feb 23, 2010 · Viewed 48.9k times · Source

I'm writing some code to utilise a 3rd party component, and I need to supply an object which implements ICredentials when I start to use it.

If I write the following...

var credential = new NetworkCredential("MyUsername", "MyPassword");

...and pass "credential", it's fine. But I would like to pass the credentials of the current user (it's a Windows service, so runs as a specified user).

I have tried both of the following, but neither appear to work (or return anything):

NetworkCredential credential = System.Net.CredentialCache.DefaultCredentials;
NetworkCredential credential = CredentialCache.DefaultNetworkCredentials;

Can anyone suggest how to acquire an approriate object, which represents the credentials of the username that the service is running under ?

Thanks

Answer

yamspog picture yamspog · Apr 10, 2010

have you tried WindowsIdentity.GetCurrent()?

you could also look at this example... http://www.codeproject.com/KB/vb/Windows_Service.aspx