怎么用ssl套接字进行socket连接,以下代码哪里出了问题?

jark2008 2012-11-28 06:11:49
如题。
我用ssl进行socket连接,出错。
错误如下:
[Win32Exception (0x80004005): 处理证书时,出现了一个未知错误。]
[AuthenticationException: 调用 SSPI 失败,请参见内部异常。]
System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) +1117335
System.Net.Security.SslState.get_SecureStream() +17
System.Net.Security.SslStream.Write(Byte[] buffer) +15
WebApplication4.cnnic.Default.connect() in E:\Visual Studio 2008\Projects\WebApplication4\WebApplication4\cnnic\Default.aspx.cs:72
WebApplication4.cnnic.Default.Page_Load(Object sender, EventArgs e) in E:\Visual Studio 2008\Projects\WebApplication4\WebApplication4\cnnic\Default.aspx.cs:33
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
代码如下:
using System;
using System.Text;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security.Authentication;

namespace WebApplication4.cnnic
{
public partial class Default : System.Web.UI.Page
{
public Socket skClient;
public string host;
public int port;
private int receiveN;

protected void Page_Load(object sender, EventArgs e)
{
host = "sheep.cnnic.cn";
port = 4121;
connect();
}

public void connect()
{

X509CertificateCollection certs = new X509CertificateCollection();
X509Certificate cert = new X509Certificate(Server.MapPath("/cnnic/szbz02kw.crt"), "szbzszbz");
certs.Add(cert);
TcpClient client = new TcpClient(host, port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(host, certs, SslProtocols.Tls, true);
}
catch (AuthenticationException e)
{

}
byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
ReadMessage(sslStream);

}

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
else
{
return false;
}
}

static string ReadMessage(SslStream sslStream)
{
byte[] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
int bytes = -1;
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
if (messageData.ToString().IndexOf("<EOF>") != -1)
{
break;
}
} while (bytes != 0);

return messageData.ToString();
}
}
}
...全文
433 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshi123 2012-11-30
  • 打赏
  • 举报
回复
在捕获验证异常的地方加上throw,看看报的什么错 catch (AuthenticationException e) { throw; }
jark2008 2012-11-29
  • 打赏
  • 举报
回复
不要沉。求人解答!!!
jark2008 2012-11-29
  • 打赏
  • 举报
回复
我是楼主。 我有互联网中心给的一个公钥和一个私钥。他们的技术支持说公钥和私钥都要使用。请问,怎么用?这个代码只使用了一个私钥,是不是不对?正确的代码应该怎么写?
段传涛 2012-11-29
  • 打赏
  • 举报
回复
身份验证的问题。 检查下权限试试。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧