RequestMessage.cs 2.3 KB

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