2
0

NoneAuthenticationMethod.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using Renci.SshNet.Messages.Authentication;
  5. using Renci.SshNet.Messages;
  6. namespace Renci.SshNet
  7. {
  8. /// <summary>
  9. /// Provides functionality for "none" authentication method
  10. /// </summary>
  11. public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
  12. {
  13. private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
  14. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  15. /// <summary>
  16. /// Gets connection name
  17. /// </summary>
  18. public override string Name
  19. {
  20. get { return "none"; }
  21. }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="NoneAuthenticationMethod"/> class.
  24. /// </summary>
  25. /// <param name="username">The username.</param>
  26. /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or null.</exception>
  27. public NoneAuthenticationMethod(string username)
  28. : base(username)
  29. {
  30. }
  31. /// <summary>
  32. /// Authenticates the specified session.
  33. /// </summary>
  34. /// <param name="session">The session.</param>
  35. /// <returns>
  36. /// Result of authentication process.
  37. /// </returns>
  38. /// <exception cref="System.ArgumentNullException"><paramref name="session" /> is null.</exception>
  39. public override AuthenticationResult Authenticate(Session session)
  40. {
  41. if (session == null)
  42. throw new ArgumentNullException("session");
  43. session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
  44. session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;
  45. session.SendMessage(new RequestMessageNone(ServiceName.Connection, Username));
  46. session.WaitOnHandle(_authenticationCompleted);
  47. session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived;
  48. session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived;
  49. return _authenticationResult;
  50. }
  51. private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e)
  52. {
  53. _authenticationResult = AuthenticationResult.Success;
  54. _authenticationCompleted.Set();
  55. }
  56. private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  57. {
  58. if (e.Message.PartialSuccess)
  59. _authenticationResult = AuthenticationResult.PartialSuccess;
  60. else
  61. _authenticationResult = AuthenticationResult.Failure;
  62. // Copy allowed authentication methods
  63. AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
  64. _authenticationCompleted.Set();
  65. }
  66. #region IDisposable Members
  67. private bool _isDisposed;
  68. /// <summary>
  69. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  70. /// </summary>
  71. public void Dispose()
  72. {
  73. Dispose(true);
  74. GC.SuppressFinalize(this);
  75. }
  76. /// <summary>
  77. /// Releases unmanaged and - optionally - managed resources
  78. /// </summary>
  79. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  80. protected virtual void Dispose(bool disposing)
  81. {
  82. // Check to see if Dispose has already been called.
  83. if (!_isDisposed)
  84. {
  85. // If disposing equals true, dispose all managed
  86. // and unmanaged resources.
  87. if (disposing)
  88. {
  89. // Dispose managed resources.
  90. if (_authenticationCompleted != null)
  91. {
  92. _authenticationCompleted.Dispose();
  93. _authenticationCompleted = null;
  94. }
  95. }
  96. // Note disposing has been done.
  97. _isDisposed = true;
  98. }
  99. }
  100. /// <summary>
  101. /// Releases unmanaged resources and performs other cleanup operations before the
  102. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  103. /// </summary>
  104. ~NoneAuthenticationMethod()
  105. {
  106. // Do not re-create Dispose clean-up code here.
  107. // Calling Dispose(false) is optimal in terms of
  108. // readability and maintainability.
  109. Dispose(false);
  110. }
  111. #endregion
  112. }
  113. }