Skip to content

Commit 4e9cead

Browse files
authored
Add example for host key validation.
1 parent 5128b16 commit 4e9cead

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ This project was inspired by **Sharp.SSH** library which was ported from java an
5959
* Universal Windows Platform 10
6060

6161
## Usage
62-
Establish an SFTP connection using both password and public-key authentication:
62+
63+
### Multi-factor authentication
64+
65+
Establish a SFTP connection using both password and public-key authentication:
6366

6467
```cs
6568
var connectionInfo = new ConnectionInfo("sftp.foo.com",
@@ -73,6 +76,40 @@ using (var client = new SftpClient(connectionInfo))
7376

7477
```
7578

79+
### Verify host identify
80+
81+
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:
82+
83+
```cs
84+
byte[] expectedFingerPrint = new byte[] {
85+
0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
86+
0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
87+
};
88+
89+
using (var client = new SshClient("sftp.foo.com", "guest", "pwd"))
90+
{
91+
client.HostKeyReceived += (sender, e) =>
92+
{
93+
if (expectedFingerPrint.Length != e.FingerPrint.Length)
94+
{
95+
for (var i = 0; i < expectedFingerPrint.Length; i++)
96+
{
97+
if (expectedFingerPrint[i] != e.FingerPrint[i])
98+
{
99+
e.CanTrust = false;
100+
break;
101+
}
102+
}
103+
}
104+
else
105+
{
106+
e.CanTrust = false;
107+
}
108+
};
109+
client.Connect();
110+
}
111+
```
112+
76113
## Building SSH.NET
77114

78115
Software | net35 | net40 | netstandard1.3 | sl4 | sl5 | wp71 | wp8 | uap10.0 |

0 commit comments

Comments
 (0)