Getting Networkcredential for Current User (C#)

Getting NetworkCredential for current user (C#)

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");

Obtain Network Credentials from Current User in Windows Authentication Application

CredentialCache.DefaultNetworkCredentials?

The credentials returned by DefaultNetworkCredentials represents the authentication credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application.

C# ASP .NET; Get the NetworkCredential Object for the logged in user?

FYI,
I think I've managed to solve the issue;
I'm using an impersonation context, and I use the CredentialCache.DefaultNetworkCredentials, and I set the WebClient's UseDefaultCredentials to true, and this seems to be working so far.
So:

WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
using (identity.Impersonate())
{
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
webClient.UseDefaultCredentials = true;
}

This worked for me.

How to provide user name and password when connecting to a network share

You can either change the thread identity, or P/Invoke WNetAddConnection2. I prefer the latter, as I sometimes need to maintain multiple credentials for different locations. I wrap it into an IDisposable and call WNetCancelConnection2 to remove the creds afterwards (avoiding the multiple usernames error):

using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
File.Copy(@"\\server\read\file", @"\\server2\write\file");
}

Get current user network credentials from within WWF custom activity

Nope. I believe that you would not be able to do that. All the async jobs are done in security context of account that is used for AsyncService login.

Impersonation and NetworkCredential

A somewhat lengthy article in MSDN explaining the options to obtain network credentials in ASP:

How To: Use Impersonation and Delegation in ASP.NET 2.0

Another blog article on the topic (though I didn't check whether the solution actually works:

.NET (C#) Impersonation with Network Credentials



Related Topics



Leave a reply



Submit