简体   繁体   English

模仿和NetworkCredential

[英]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. 我需要将具有当前模拟用户凭据的NetworkCredential对象从asp.net应用程序传递给Web服务。
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. 我已经了解CredentialCache.DefaultNetworkCredentials应该提供当前模拟用户的凭据,但事实并非如此。
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? 如果是这样,您如何获得当前模拟用户的NetworkCredential对象?

A somewhat lengthy article in MSDN explaining the options to obtain network credentials in ASP: MSDN中有点冗长的文章,解释了在ASP中获取网络凭据的选项:

How To: Use Impersonation and Delegation in ASP.NET 2.0 如何:在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 .NET(C#)使用网络凭据进行模拟

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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