- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建客户端-服务器应用程序。
服务器代码:
public sealed class SslTcpServer {
static X509Certificate serverCertificate = null;
public static void RunServer(string certificate) {
serverCertificate = new X509Certificate2(certificate, "123", X509KeyStorageFlags.MachineKeySet);
TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 56748);
listener.Start();
while (true) {
Console.WriteLine("Waiting for a client to connect...");
TcpClient client = listener.AcceptTcpClient();
ProcessClient(client);
}
}
static void ProcessClient(TcpClient client) {
SslStream sslStream = new SslStream(client.GetStream());
try {
sslStream.AuthenticateAsServer(serverCertificate,
clientCertificateRequired: false,
checkCertificateRevocation: true);
sslStream.ReadTimeout = 5000;
sslStream.WriteTimeout = 5000;
Console.WriteLine("Waiting for client message...");
string messageData = ReadMessage(sslStream);
Console.WriteLine("Received: {0}", messageData);
byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
Console.WriteLine("Sending hello message.");
sslStream.Write(message);
} catch (AuthenticationException e) {
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null) {
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
sslStream.Close();
client.Close();
return;
} finally {
sslStream.Close();
client.Close();
}
}
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();
}
public static int Main(string[] args) {
string certificate = "server.crt";
RunServer(certificate);
return 0;
}
}
客户代码:
public class SslTcpClient {
private static Hashtable certificateErrors = new Hashtable();
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors) {
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
return false;
}
public static void RunClient(string machineName, string serverName) {
TcpClient client = new TcpClient("127.0.0.1", 56748);
Console.WriteLine("Client connected.");
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
try {
sslStream.AuthenticateAsClient("127.0.0.1");
} catch (AuthenticationException e) {
Console.WriteLine("Exception: {0}", e.Message);
Console.WriteLine(e.StackTrace);
if (e.InnerException != null) {
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
string serverMessage = ReadMessage(sslStream);
Console.WriteLine("Server says: {0}", serverMessage);
client.Close();
Console.WriteLine("Client closed.");
}
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();
}
public static int Main(string[] args) {
string serverCertificateName = "127.0.0.1";
string machineName = "127.0.0.1";
SslTcpClient.RunClient(machineName, serverCertificateName);
return 0;
}
}
我已经创建了 SSL 证书“server.crt”,但我无法将用户连接到服务器。
我有一个未处理的异常:
System.NotSupportedException: The server mode SSL must use a certificate with the associated private key.
最佳答案
异常告诉您您还需要服务器证书的私钥,因为 SSL 服务器需要它来解密客户端生成的 session key 。现在,话虽如此,您传递给 RunServer 方法的证书是 Server.crt,它只是一个公共(public)证书(即它没有关联的私钥)。如果您自己生成了此证书,您还应该生成一个 server.p12 或 server.pfx 文件(其中也有私钥),然后从中创建一个 X509Certificate。像这样的..
var certificate = new X509Certificate("server.pfx", "password_of_the_private_key");
关于C#。证书。系统.NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53370592/
目前,我有一个在我的 Mac 上运行的服务,该服务是用 C# 编写的,位于 usr/local/program。该服务能够通过根据服务器版本号检查版本号来检测何时需要更新。更新方法已运行,但是当它尝试
我们有一个下游系统,一个 ERP 系统,它可以通过 SOAP 公开其表。它发布的 Web 服务通常具有 Create、Update 和 Delete 方法。然后我们使用 svcutil 生成代理,包括
我在 Windows 商店应用程序中处理 SQLite。我正在使用更新表中的值 var tagPage = db.QueryAsync("UPDATE MyModel SET Tag =2 WHERE
我一直在努力使用 Moq 作为模拟框架并复制了一些非常简单的示例代码。我一定在这里遗漏了一些非常愚蠢的东西。它在 Setup 调用上抛出 NotSupportedException,即使它指向 Ret
为什么会抛出 System.NotSupportedException? string foo(string f) { return f; } string bar = ""; var item =
我开始遇到这个非常奇怪的问题,该应用程序在 iOS 模拟器上运行良好,但是当我在设备上启动它时,它显示登录屏幕,但在登录后,它崩溃并显示“不支持操作” “异常(exception)。堆栈跟踪是完全不可
有谁知道有什么方法可以克服 NotSupportedException?我有一个针对用户的方法: public virtual bool IsAbove(User otherUser) {
我正在对使用 IMemoryCache 的 ClientService 进行单元测试界面: 客户端服务.cs: public string Foo() { //... code
因此,我尝试在 UserDatabse.cs 类中使用此方法检索特定日期的“Sugar”类的实例: public List GetSugarDate(DateTime dt) { var
我需要在忽略大小写的情况下从数据库上下文中查找项目,但无法正常工作,到目前为止我尝试过的是: var interestingItem = ctx.MyItems.FirstOrDefault(x =>
我有这条路径可以根据日期和时间保存 Excel 工作表。它正在显示System.NotSupportedException 什么是正确的格式? string strpath = Server.M
我正在尝试返回状态更新列表,该列表按插入日期降序排列。此列表显示在局部 View 中,因此我无法使用 Controller 对列表进行排序。解决方案是创建一个包含以下代码的服务类: var Statu
如果我有一个尚未实现基类提供的函数的子类,我可以覆盖该函数并让它抛出一个NotSupportedException。有没有办法为此生成编译时错误以避免仅在运行时命中它? 更新:我不能使基类抽象。 最佳
我有一个如下所示的查询: var caseList = (from x in context.Cases where allowedCaseIds.Contains(x => x.C
我正在创建客户端-服务器应用程序。 服务器代码: public sealed class SslTcpServer { static X509Certificate serverCertifi
我正在构建一个本地化目录,但遇到了设计难题。现在,目录存储一个 Dictionary存储翻译,其中 IString可以是两种类型:Singular或 Plural .这是 IString 的简化版本:
我一直在做一个需要通过 JSON 文件保存和加载数据的项目。此 JSON 文件包含其他对象的各种列表。但是,当我继续反序列化文件时,会发生这种情况: System.NotSupportedExcept
File.ReadAllText 似乎有问题,因为即使目标文件存在,它也会返回“NotSupportedException”。无论将什么放入参数中,它都会不断抛出相同的异常。 using System
您能否帮助解决此错误的问题或说明如何以不同的方式完成此操作。谢谢! 我的任务是输出结果(集合),其中包含每个实体的注释。如果用户在选中它。 protected Expression > MapToB
LINQ 到 EF: db.ReadonlyQuery() .Select(t => new ACurrentDayInfo { OrderI
我是一名优秀的程序员,十分优秀!