ASP.NET中的NetworkCredential错误

Seb*_*Seb 6 c# asp.net webclient cryptographicexception networkcredentials

我正在尝试使用NetworkCredential类通过ASP.NET访问网页.但是我继续通过以下消息获得异常System.Security.Cryptography.CryptographicException: The handle is invalid

下面是关于我如何调用该函数的代码.任何帮助是极大的赞赏.

C#:

System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = new System.Net.NetworkCredential("Admin", "Nimda");
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

[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
Run Code Online (Sandbox Code Playgroud)

BNe*_*ula 5

我刚刚遇到同样的问题.我在VS 2010中的ASP.NET开发服务器下本地发生.当我在Windows 7/IIS 7下执行应用程序时,它运行得很好.希望这可以帮助!

  • 对我来说,我停止了开发服务器并在VS 2010中重新启动了调试器(也重新启动了开发服务器),然后它运行正常. (3认同)

Rus*_*rke 4

好吧,这有点尴尬 - 我已经看过了,错误是由于你的 Windows 配置......某处。

抛出异常的代码部分实际上是对 advapi32.dll 中函数的互操作调用,具体来说:

int status = Win32Native.SystemFunction040(this.m_buffer, (uint) (this.m_buffer.Length * 2), 0);
if (status < 0)
{
    throw new CryptographicException(Win32Native.LsaNtStatusToWinError(status));
}
this.m_encrypted = true;
Run Code Online (Sandbox Code Playgroud)

来电:

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
internal static extern int SystemFunction040([In, Out] SafeBSTRHandle pDataIn, [In] uint cbDataIn, [In] uint dwFlags);
Run Code Online (Sandbox Code Playgroud)

这会返回错误代码,导致出现异常。

如果您在工作场所,您可能需要与系统管理员/网络人员交谈,看看您的本地策略中是否有任何内容可能导致失败。否则,我会看看如果您禁用防病毒/禁用防火墙/关闭任何第三方代理软件会发生什么。

基本上,任何覆盖默认网络功能的东西。另外,可能值得检查您是否拥有所有最新的 Windows 更新,并且没有任何病毒或恶意软件感染。

抱歉,我不能更具体,但我不认为这是 .Net/编程错误。