PrivateKey.aml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <topic id="c233e412-0ea1-422f-8337-e5d1231b71e5" revisionNumber="1">
  3. <developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
  4. <introduction>
  5. <para>
  6. This topic describes hot to connect to the remote host using private key file.
  7. <!--
  8. To connect to remote server using private key you need to provide <codeEntityReference>T:Renci.SshNet.PrivateKeyFile</codeEntityReference> object which points to file that contains private key information.-->
  9. </para>
  10. <list class="bullet">
  11. <listItem>
  12. <para>
  13. <link xlink:href="#PrivateKeyGenerationSection">Private Key file generation.</link>
  14. </para>
  15. </listItem>
  16. <listItem>
  17. <para>
  18. <link xlink:href="#PrivateKeyExamplesSection">Private Key file examples.</link>
  19. </para>
  20. </listItem>
  21. <listItem>
  22. <para>
  23. <link xlink:href="#CreatePrivateKeyFileInstanceSection">Create PrivateKeyFile instance.</link>
  24. </para>
  25. </listItem>
  26. <listItem>
  27. <para>
  28. <link xlink:href="#UsePrivateKeyFileInstanceSection">Using PrivateKeyFile instance.</link>
  29. </para>
  30. </listItem>
  31. <listItem>
  32. <para>
  33. <link xlink:href="#MultiplePrivateKeyFilesSection">Using multiple private key files.</link>
  34. </para>
  35. </listItem>
  36. </list>
  37. </introduction>
  38. <section address="PrivateKeyGenerationSection">
  39. <title>Private Key file generation</title>
  40. <content>
  41. <para>
  42. Before using private key for authentication you need to create one and configure remote host to accept it.
  43. Typically it can be done by executing following command on the remote host.
  44. </para>
  45. <code language="text" title="Private key generation example">
  46. ssh-keygen -t rsa
  47. </code>
  48. </content>
  49. </section>
  50. <section address="PrivateKeyExamplesSection">
  51. <title>Private Key file examples</title>
  52. <content>
  53. <para>
  54. To use private key for authentication, private key must be generated. Typecally it can be done un remote host by issue <command>ssh-keygen -t rsa</command> command.
  55. Below are some private key examples:
  56. </para>
  57. <code language="text" title="Private RSA key example">
  58. <![CDATA[
  59. -----BEGIN RSA PRIVATE KEY-----
  60. MIIEoQIBAAKCAQEAuTtXn+BatX1oJuvhqfJZw5jc/pcIxJUPmuoFCH3+bXfKBJ/9
  61. 4ixNETzZBasyvT/ozboAbCG3qcJOYxf2BEeTAIXe1jLAoTd1GKCwMvZOyjnsPN95
  62. /lChwfdnBbMzpZYTGfoUylXme/mzjjLu/J0qXgR5lyk9HFT+x5YEtRl8VSHiDkLK
  63. TZ37dwhsqgcs+PkfvYMUK+C8evnfE0tgWgKZk0Eatl87nLWyVXB4LzhSDtGKLCPA
  64. OgrX7fYfplDwJ2WK1N6nG0FnxW1HhDeSK7e2TbAa2vZQgvFXMWnO4O/NZKp4COpO
  65. ReyliWhdtKAjr/+cD4yDfPjhjjKOYfxbvdRG4QIBIwKCAQAqVrTxV9o4HKoXhl93
  66. TVZYl/f/rX5Y0Z0quSW4zFdpendRg6e+qwpNFTjrWlS9ivNiOSSrAGR+ktAWpmQe
  67. PD7bjFAw9ahfXSIUQfxja3+5Mc+Y4p+KlhZYOIyTlqy4Ik2CR8o84G8yR7QDPteK
  68. Mo1XUXrguPgGedPV2SWlvK60XyAXqsewDhi7SeImZomKzbh33SXjVxakzHfa8BEU
  69. eIIeR9oFlQMuYdo4GrHhFO2T+g/gqw/kVd1zkeEwt06fZVDErVwp+twewxxvwrk4
  70. CKUCzavfhDfi5sJ5YdzhDBRgkyBgJI+f15dKyqqOiAparV9+uzrD6vIuNnlVoqQA
  71. iugLAoGBAPBliy32e83nshBknBn5HOK2rO3a1zHxvYr/NzITXtdZOjatNyfXtkwi
  72. Ll/el5tZhJvKe9nItSI/4w7mvlvXZfW8h3MR0qb8at4jWa8ya2hwEerqaJonqjjb
  73. +eBhg27ltZIQRk8Bv6ApXTAWkc+dFGhEIysokDQX7V72Bdrizup1AoGBAMVBLHK0
  74. 5IFb8x7danlAmDX6bqCObId4Pce2OeONFIj1jIowvCXaE0t9zU4X5SdN5ujqu4Dq
  75. XgzUdNeKcJxWpFO74MDRxT3CbMz36fikJnvxWl/+q0HalYuCY8gm14VYcThUBAro
  76. 3c941INueybGNLIA9jc7RMnsFtyVTvNYpaU9AoGAFJr9TRUgjf3qsPKuS15+0Zqh
  77. G7OsC5hgtCSBEuu3rA72XHU/Pe3rDdcLSgvD2h2dpvQZPo2L3l0/WQx2t2o78H3f
  78. uWftfAcB2Iav6nIJNNZn75BvXaug4E1ej5NUaJdYtL+Q/3UtrqR1s6opwVabWWTt
  79. ElPvGmhzboodwk30en8CgYAyuPzNCfGdm00lMZ8JPH7pTwaBDq4xdrDM9FgHUCna
  80. E0FlXP0uTgT2J6nSQKijtPI75JadfhgvL1E+vTLmX2wViBU45XvcrlZ92Vlr0nBL
  81. wbgnUB1otIzauyD49AuIsFegxSWcZ8QCJmKIMlouir0X1FyR3Apfzv6Qfio+kyNH
  82. vwKBgQCtwxojkzUSfV3zDt6bYSLBzgXgo/Zr9lS+gSggP72DzINmW2gbA0fkM2Zu
  83. JltcfakKv4gVX/1zooz+7t+4bj6dqt+bl7hYz0VnTSDZGuo5LKDif/4gSGrdblC2
  84. QLTuX2HjWCZdsue7mRwL7cXR4zlIoE99+Ryhdxvc5wHSfYr/JA==
  85. -----END RSA PRIVATE KEY-----
  86. ]]>
  87. </code>
  88. </content>
  89. </section>
  90. <section address="CreatePrivateKeyFileInstanceSection">
  91. <title>Create PrivateKeyFile instance</title>
  92. <content>
  93. <para>
  94. To specify private key you need to intsantiate <codeEntityReference>T:Renci.SshNet.PrivateKeyFile</codeEntityReference> object.
  95. Some private key require passphrase to be specified.
  96. <alert class="note">
  97. <para>
  98. If private key requires a passphrase but it was not specified then <codeEntityReference>T:Renci.SshNet.Common.SshPassPhraseNullOrEmptyException</codeEntityReference> exception will be thrown.
  99. </para>
  100. </alert>
  101. </para>
  102. <code language="cs" title="Create PrivateKeyFile without providing passphrase">
  103. var keyFile = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key.txt"));
  104. </code>
  105. <code language="cs" title="Create PrivateKeyFile by providing passphrase">
  106. var keyFile = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key.txt"), "tester")
  107. </code>
  108. </content>
  109. </section>
  110. <section address="UsePrivateKeyFileInstanceSection">
  111. <title>Using PrivateKeyFile instance</title>
  112. <content>
  113. <para>
  114. You can pass <codeEntityReference>T:Renci.SshNet.PrivateKeyFile</codeEntityReference> object to client constructor or when creating <codeEntityReference>T:Renci.SshNet.PrivateKeyConnectionInfo</codeEntityReference> object.
  115. </para>
  116. <code language="cs" title="Provide PrivateKeyFile instance to PrivateKeyConnectionInfo example">
  117. var keyFile = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key.txt"));
  118. var connectionInfo = new PrivateKeyConnectionInfo("server", 22, "tester", keyFile);
  119. </code>
  120. <code language="cs" title="Provide PrivateKeyFile instance to SshClient example">
  121. var keyFile = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key.txt"), "tester")
  122. using (var client = new SshClient("host", "username", keyFile))
  123. {
  124. }
  125. </code>
  126. </content>
  127. </section>
  128. <section address="MultiplePrivateKeyFilesSection">
  129. <title>Using multiple private key files</title>
  130. <content>
  131. <para>
  132. In some scenarious you might want to provide multiple private key to authenticate user. In this case you create PrivateKeyFile as described <link xlink:href="#CreatePrivateKeyFileInstanceSection">here</link> and then pass those file to either client or <codeEntityReference>T:Renci.SshNet.PrivateKeyConnectionInfo</codeEntityReference> constructor.
  133. </para>
  134. <code language="cs" title="Provide multiple PrivateKeyFile instances example">
  135. var keyFile1 = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key1.txt"));
  136. var keyFile2 = new PrivateKeyFile(File.OpenRead(@"rsa_pass_key2.txt"));
  137. var connectionInfo = new PrivateKeyConnectionInfo("server", 22, "tester", keyFile1, keyFile2);
  138. </code>
  139. </content>
  140. </section>
  141. <!-- <content>
  142. <para>
  143. Some private key are encrypted using passphrase. You can specify passphrase by creating private key using <codeEntityReference qualifyHint="true" autoUpgrade="true">M:Renci.SshNet.PrivateKeyFile.#ctor(Stream, String)</codeEntityReference> or <codeEntityReference qualifyHint="true" autoUpgrade="true">M:Renci.SshNet.PrivateKeyFile.#ctor(System.String, System.String)</codeEntityReference> constructors.
  144. Test <codeEntityReference qualifyHint="true" autoUpgrade="true">M:Renci.SshNet.PrivateKeyFile.#ctor</codeEntityReference>
  145. </para>
  146. <codeExample>
  147. <list class="ordered">
  148. <listItem>
  149. <para>Providing private key directly in the client constructor</para>
  150. <code language="cs">
  151. using (var client = new SshClient("host", "username", new PrivateKeyFile(File.OpenRead(@"private.key"))))
  152. {
  153. client.Connect();
  154. client.Disconnect();
  155. }
  156. </code>
  157. </listItem>
  158. <listItem>
  159. <para>Providing private key and passphrase directly in the client constructor</para>
  160. <code language="cs">
  161. using (var client = new SshClient("host", "username", new PrivateKeyFile(File.OpenRead(@"private.key"), "passphrase")))
  162. {
  163. client.Connect();
  164. client.Disconnect();
  165. }
  166. </code>
  167. </listItem>
  168. <listItem>
  169. <para>Providing private key and passphrase directly using <codeEntityReference>T:Renci.SshNet.PrivateKeyConnectionInfo</codeEntityReference> object.</para>
  170. <code language="cs">
  171. var connectionInfo = new PrivateKeyConnectionInfo("host", 1234, "username", new PrivateKeyFile(File.OpenRead(@"rsa_pass_key.txt"), "tester"));
  172. using (var client = new SshClient(connectionInfo))
  173. {
  174. client.Connect();
  175. client.Disconnect();
  176. }
  177. </code>
  178. </listItem>
  179. </list>
  180. </codeExample>
  181. </content>
  182. -->
  183. <relatedTopics>
  184. <codeEntityReference>T:Renci.SshNet.PrivateKeyConnectionInfo</codeEntityReference>
  185. <codeEntityReference>T:Renci.SshNet.SshClient</codeEntityReference>
  186. <codeEntityReference>M:Renci.SshNet.SshBaseClient.Connect()</codeEntityReference>
  187. </relatedTopics>
  188. </developerConceptualDocument>
  189. </topic>