ASP.NET 中的网络凭据错误

发布于 2024-11-06 22:00:04 字数 797 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫罗兰の梦幻 2024-11-13 22:00:04

我刚刚遇到了同样的问题。我在 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!

慵挽 2024-11-13 22:00:04

好吧,这有点尴尬 - 我已经看过了,错误是由于你的 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);

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

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

基本上,任何覆盖默认网络功能的东西。
另外,可能值得检查您是否拥有所有最新的 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:

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;

Calls:

[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);

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.

书间行客 2024-11-13 22:00:04

这应该在标准的 powershell 控制台中工作:

Add-Type -AssemblyName System.Net
$client = New-Object System.Net.WebClient
$netc = New-Object System.Net.NetworkCredential("Admin", "Nimda")
$client.Credentials = $netc

看看这是否会生成相同的无效句柄错误将会很有趣。

编辑:抱歉,第二行有错字。

This should work in a standard powershell console:

Add-Type -AssemblyName System.Net
$client = New-Object System.Net.WebClient
$netc = New-Object System.Net.NetworkCredential("Admin", "Nimda")
$client.Credentials = $netc

Will be interesting to see if this generates the same invalid handle error.

Edit: Apologies, there was a typo in the 2nd line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文