我需要从asp.net应用程序向web服务传递一个带有当前模拟用户凭据的NetworkCredential
对象。
我的代码如下:
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();
}
我知道
CredentialCache.DefaultNetworkCredentials
应该提供当前模拟用户的凭据,但事实并非如此。我收到的日志消息是
WindowsIdentity = TESTDOMAIN\TESTUSER
DefaultNetworkCredentials =
我做错什么了吗?如果是,如何获取当前模拟用户的NetworkCredential对象?
最佳答案
msdn中的一篇有点冗长的文章解释了在asp中获取网络凭据的选项:
How To: Use Impersonation and Delegation in ASP.NET 2.0
关于这个主题的另一篇博客文章(尽管我没有检查解决方案是否真的有效:
.NET (C#) Impersonation with Network Credentials