DefaultNetworkCredentials 或 DefaultCredentials
当我需要向代理(本地或网络中)提供凭据时,我应该使用哪一个?
这两者之间的确切区别是什么?
Which one am I supposed to use when I need to supply a credential to a proxy (local or in Network)?
What's the exact difference between these two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们是完全相同的东西,您可以使用像 Reflector 这样的反汇编器亲自确认这一点。唯一的区别是
DefaultNetworkCredentials
返回一个NetworkCredentials
对象,并且DefaultCredentials
将其转换为ICredentials
。因此,您可以使用 NetworkCredentials 对象访问更多信息,但是您使用哪一个提供给需要 ICredentials 实例的对象没有区别,因为它是相同的对象实例:object.ReferenceEquals(CredentialCache.DefaultCredentials, CredentialCache. DefaultNetworkCredentials) 返回true
。They are exactly the same thing, which you can confirm for yourself using a disassembler like Reflector. The only difference is that
DefaultNetworkCredentials
returns aNetworkCredentials
object and andDefaultCredentials
casts it toICredentials
. So you have access to more information with a NetworkCredentials object, but which of those you use supply to an object requiring an ICredentials instance makes no difference, since it's the same object instance:object.ReferenceEquals(CredentialCache.DefaultCredentials, CredentialCache.DefaultNetworkCredentials)
returnstrue
.两者之间的差异非常微妙。 DefaultNetworkCredentials 是两者中较新的一个(.NET 2.0 中添加的),其核心区别在于,在某些安全条件下,它可以向应用程序公开有关登录用户的更多隐私信息。有关详细信息,请尝试此博客文章:
http://blogs. msdn.com/buckh/archive/2004/07/28/199706.aspx
The difference between the two is very subtle. DefaultNetworkCredentials is the newer of the two (added with .NET 2.0), and the core difference is that under certain security conditions, it can expose more private information about the logged-in user to the application. For more information, try this blog post:
http://blogs.msdn.com/buckh/archive/2004/07/28/199706.aspx