Sfoglia il codice sorgente

Add example for host key validation.

Gert Driesen 8 anni fa
parent
commit
4e9ceadfd3
1 ha cambiato i file con 38 aggiunte e 1 eliminazioni
  1. 38 1
      README.md

+ 38 - 1
README.md

@@ -59,7 +59,10 @@ This project was inspired by **Sharp.SSH** library which was ported from java an
 * Universal Windows Platform 10
 
 ## Usage
-Establish an SFTP connection using both password and public-key authentication:
+
+### Multi-factor authentication
+
+Establish a SFTP connection using both password and public-key authentication:
 
 ```cs
 var connectionInfo = new ConnectionInfo("sftp.foo.com",
@@ -73,6 +76,40 @@ using (var client = new SftpClient(connectionInfo))
 
 ```
 
+### Verify host identify
+
+Establish a SSH connection using user name and password, and reject the connection if the fingerprint of the server does not match the expected fingerprint:
+
+```cs
+byte[] expectedFingerPrint = new byte[] {
+                                            0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
+                                            0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
+                                        };
+
+using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
+{
+    client.HostKeyReceived += (sender, e) =>
+        {
+            if (expectedFingerPrint.Length != e.FingerPrint.Length)
+            {
+                for (var i = 0; i < expectedFingerPrint.Length; i++)
+                {
+                    if (expectedFingerPrint[i] != e.FingerPrint[i])
+                    {
+                        e.CanTrust = false;
+                        break;
+                    }
+                }
+            }
+            else
+            {
+                e.CanTrust = false;
+            }
+        };
+    client.Connect();
+}
+```
+
 ## Building SSH.NET
 
 Software                          | net35 | net40 | netstandard1.3 | sl4 | sl5 | wp71 | wp8 | uap10.0 |