ASP.NET 中的网络凭据错误
我正在尝试使用 NetworkCredential 类通过 ASP.NET 访问网页。但是,我不断收到异常,并显示以下消息 System.Security.Cryptography.CryptographicException:句柄无效
下面是有关如何尝试调用该函数的代码。非常感谢任何帮助。
C#:
System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = new System.Net.NetworkCredential("Admin", "Nimda");
堆栈跟踪
[CryptographicException: The handle is invalid.
]
System.Security.SecureString.ProtectMemory() +154
System.Security.SecureString.InitializeSecureString(Char* value, Int32 length) +170
System.Security.SecureString..ctor(Char* value, Int32 length) +65
System.Net.SecureStringHelper.CreateSecureString(String plainString) +6181188
System.Net.NetworkCredential..ctor(String userName, String password) +64
I am trying to access a webpage through ASP.NET using the NetworkCredential class. However I keep getting an exception with the following message System.Security.Cryptography.CryptographicException: The handle is invalid
Below is my code on how I am trying to call the function. Any help is greatly appreciated.
C#:
System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = new System.Net.NetworkCredential("Admin", "Nimda");
Stack Trace
[CryptographicException: The handle is invalid.
]
System.Security.SecureString.ProtectMemory() +154
System.Security.SecureString.InitializeSecureString(Char* value, Int32 length) +170
System.Security.SecureString..ctor(Char* value, Int32 length) +65
System.Net.SecureStringHelper.CreateSecureString(String plainString) +6181188
System.Net.NetworkCredential..ctor(String userName, String password) +64
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题。我在 VS 2010 中的 ASP.NET 开发服务器下进行本地操作。当我在 Windows 7 / IIS 7 下执行该应用程序时,它工作得很好。希望这有帮助!
I've just hit this same problem. I was happening locally under the ASP.NET Development Server in VS 2010. When I executed the app under my Windows 7 / IIS 7, it worked just fine. Hope this helps!
好吧,这有点尴尬 - 我已经看过了,错误是由于你的 Windows 配置......某处。
抛出异常的代码部分实际上是对 advapi32.dll 中函数的互操作调用,具体来说:
调用:
返回错误代码,导致异常。
如果您在工作场所,您可能需要与系统管理员/网络人员交谈,看看您的本地策略中是否有任何内容可能导致失败。
否则,我会看看如果您禁用防病毒/禁用防火墙/关闭任何第三方代理软件会发生什么。
基本上,任何覆盖默认网络功能的东西。
另外,可能值得检查您是否拥有所有最新的 Windows 更新,并且您没有感染任何病毒或恶意软件。
抱歉,我不能更具体,但我不认为这是 .Net/编程错误。
Ok this is a bit awkward - I've had a look and the error is down to your windows configuration .... somewhere.
The part of the code that's throwing an Exception is actually an interop call to a function in advapi32.dll, specifically:
Calls:
That's returning an error code, causing your exception.
If you're in a workplace, you might want to talk with your sysadmin/network guys to see if there's anything in your local policies that might cause a failure.
Otherwise, I'd see what happens if you disable your Anti-virus/disable firewall/turn of any 3rd party proxy software.
Basically, anything that overrides default network functionality.
Also, might be worth checking you have all the latest windows updates and that you dont have any virus or malware infection.
Sorry I can't be more specific, but I don't believe this is a .Net/programming error.
这应该在标准的 powershell 控制台中工作:
看看这是否会生成相同的无效句柄错误将会很有趣。
编辑:抱歉,第二行有错字。
This should work in a standard powershell console:
Will be interesting to see if this generates the same invalid handle error.
Edit: Apologies, there was a typo in the 2nd line.