我正在尝试使用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");

堆栈跟踪
[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

最佳答案

好的,这有点尴尬-我看过了,错误归结于您的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;

来电:
[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);

返回错误代码,导致您的异常。

如果您在工作场所中,则可能需要与sysadmin/networking伙伴联系,以查看本地策略中是否有任何可能导致故障的信息。
否则,我会看到如果您禁用防病毒/禁用防火墙/打开任何第三方代理软件会发生什么情况。

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

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

关于c# - ASP.NET中的NetworkCredential错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5950404/

10-10 13:46
查看更多