C# 模拟和网络凭据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2063408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:10:09  来源:igfitidea点击:

Impersonation and NetworkCredential

c#asp.netweb-servicesimpersonation

提问by Paolo Tedesco

I need to pass a NetworkCredentialobject with the credentials of the currently impersonated user to a web service from an asp.net application.
My code looks like this:

我需要将NetworkCredential带有当前模拟用户凭据的对象从 asp.net 应用程序传递到 Web 服务。
我的代码如下所示:

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.DefaultNetworkCredentialsshould give the credentials of the currently impersonated user, but it is not the case.
The log messages I get are

我知道CredentialCache.DefaultNetworkCredentials应该提供当前模拟用户的凭据,但事实并非如此。
我得到的日志消息是

WindowsIdentity = TESTDOMAIN\TESTUSER
DefaultNetworkCredentials = 

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

难道我做错了什么?如果是这样,您如何获得当前模拟用户的 NetworkCredential 对象?

采纳答案by Dirk Vollmar

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#) 模拟网络凭据

回答by Pent Ploompuu

It's not possible to use the asp.net impersonated user (Current.User.Identity) for network authentication, it only works locally.

无法使用 asp.net 模拟用户 ( Current.User.Identity) 进行网络身份验证,它只能在本地工作。