websocket-sharp
websocket-sharp copied to clipboard
wss communication problem
I have a C# client in Unity with Framework 3.5, and a C# Server with Framework 4.5.2 They suppose to communicate securely through wss. I created a ssl certificate.
The server security code:
var webSocketServer = new WebSocketServer(5963, true);
webSocketServer.SslConfiguration.ServerCertificate = new X509Certificate2("localhost.pfx", "Test");
webSocketServer.AuthenticationSchemes = AuthenticationSchemes.Digest;
webSocketServer.Realm = "WebSocket Test";
webSocketServer.UserCredentialsFinder = id =>
{
var name = id.Name;
return name == "userName"
? new WebSocketSharp.Net.NetworkCredential(name, "User@user", "UserDomain")
: null;
};
webSocketServer.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls;
string service = System.Configuration.ConfigurationManager.AppSettings["webSocketService"];
webSocketServer.AddWebSocketService<MyService>(service);
`webSocketServer.Start();`
The client code:
string url = "wss://localhost:5963/MyService";// single ip:4649. security ip: 5963
_webSocket = new WebSocket(url);
_webSocket.SslConfiguration.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates with an untrusted root are valid.
continue;
}
else
{
if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// If there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}
}
// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}
else
{
// In all other cases, return false.
return false;
}
};
_webSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls;
_webSocket.SetCredentials("userName", "User@user", true);
// ---------------------------------------
// invoke when the client connect to the server
_webSocket.OnOpen += (sender, e) =>
log.Info(DateTime.Now + " start comuunication");
// invoke when the server send message
_webSocket.OnMessage += (sender, e) =>
HandleMessage(e.Data);
// invoke when error occurred
_webSocket.OnError += (sender, e) =>
log.Info(DateTime.Now + " get error: " + e.Exception.Data + " " + e);
// invoke when the client unconnected to the server
_webSocket.OnClose += (sender, e) =>
log.Info(DateTime.Now + " closed " + e.Reason + " " + e);
// connect to the server
_webSocket.Connect();
Yet, when I try to make them communicate the following error occurs:
Fatal|QueueUserWorkItemCallback.WaitCallback_Context|System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm --- End of inner exception stack trace ---
Can anyone help me figure out what is the source of the problem. Thanks in advance.
Have you solved this problem friend?
i want to know too, my project is stoped cause of the same error! and i dont know what im doing wrong!