RequestMessage.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Text;
  3. namespace Renci.SshNet.Messages.Authentication
  4. {
  5. /// <summary>
  6. /// Represents SSH_MSG_USERAUTH_REQUEST message. Server as a base message for other user authentication requests.
  7. /// </summary>
  8. [Message("SSH_MSG_USERAUTH_REQUEST", 50)]
  9. public class RequestMessage : Message
  10. {
  11. /// <summary>
  12. /// Gets authentication username.
  13. /// </summary>
  14. public string Username { get; private set; }
  15. /// <summary>
  16. /// Gets the name of the service.
  17. /// </summary>
  18. /// <value>
  19. /// The name of the service.
  20. /// </value>
  21. public ServiceName ServiceName { get; private set; }
  22. /// <summary>
  23. /// Gets the name of the authentication method.
  24. /// </summary>
  25. /// <value>
  26. /// The name of the method.
  27. /// </value>
  28. public virtual string MethodName { get { return "none"; } }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="RequestMessage"/> class.
  31. /// </summary>
  32. /// <param name="serviceName">Name of the service.</param>
  33. /// <param name="username">Authentication username.</param>
  34. public RequestMessage(ServiceName serviceName, string username)
  35. {
  36. this.ServiceName = serviceName;
  37. this.Username = username;
  38. }
  39. /// <summary>
  40. /// Called when type specific data need to be loaded.
  41. /// </summary>
  42. protected override void LoadData()
  43. {
  44. throw new InvalidOperationException("Load data is not supported.");
  45. }
  46. /// <summary>
  47. /// Called when type specific data need to be saved.
  48. /// </summary>
  49. protected override void SaveData()
  50. {
  51. this.Write(this.Username);
  52. switch (this.ServiceName)
  53. {
  54. case ServiceName.UserAuthentication:
  55. this.WriteAscii("ssh-userauth");
  56. break;
  57. case ServiceName.Connection:
  58. this.WriteAscii("ssh-connection");
  59. break;
  60. default:
  61. throw new NotSupportedException("Not supported service name");
  62. }
  63. this.WriteAscii(this.MethodName);
  64. }
  65. }
  66. }