简体   繁体   中英

Impersonation and NetworkCredential

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 = TESTDOMAIN\TESTUSER
DefaultNetworkCredentials = 

Am I doing something wrong? If so, how do you get a NetworkCredential object for the currently impersonated user?

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

使用asp.net模拟用户( Current.User.Identity )进行网络身份验证是不可能的,它只能在本地运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM