Bla*_*ght 32 .net defaultnetworkcredentials
我正在编写一些代码来使用第三方组件,我需要提供一个在我开始使用它时实现ICredentials的对象.
如果我写以下内容......
var credential = new NetworkCredential("MyUsername", "MyPassword");
Run Code Online (Sandbox Code Playgroud)
...并通过"凭证",没关系.但我想传递当前用户的凭据(它是一个Windows服务,因此以指定用户身份运行).
我尝试了以下两种方法,但似乎都不起作用(或返回任何内容):
NetworkCredential credential = System.Net.CredentialCache.DefaultCredentials;
NetworkCredential credential = CredentialCache.DefaultNetworkCredentials;
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议如何获取approriate对象,该对象代表运行该服务的用户名的凭据?
谢谢,罗斯
您需要进行模拟,例如:
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
Run Code Online (Sandbox Code Playgroud)
http://support.microsoft.com/kb/306158
或者您可以使用 web.config:
<identity impersonate="true" />
Run Code Online (Sandbox Code Playgroud)