I need to pass a NetworkCredential object with the credentials of the currently impersonated user to a web service from an asp.net application.
My code looks like this:
WindowsIdentity windowsIdentity = HttpContext.Current.User.Identity as WindowsIdentity;
WindowsImpersonationContext context = windowsIdentity.Impersonate();
try {
var client = GetClient();
client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Log("WindowsIdentity = {0}", windowsIdentity.Name);
Log("DefaultNetworkCredentials = {0}", CredentialCache.DefaultNetworkCredentials.UserName);
client.DoSomething();
} finally {
context.Undo();
}
I had understood that CredentialCache.DefaultNetworkCredentials should give the credentials of the currently impersonated user, but it is not the case.
The log messages I get are
WindowsIdentity = TESTDOMAINTESTUSER DefaultNetworkCredentials =
Am I doing something wrong? If so, how do you get a NetworkCredential object for the currently impersonated user?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
A somewhat lengthy article in MSDN explaining the options to obtain network credentials in ASP:
Another blog article on the topic (though I didn’t check whether the solution actually works:
Method 2
It’s not possible to use the asp.net impersonated user (Current.User.Identity) for network authentication, it only works locally.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0