NetworkCredential.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #region License
  2. /*
  3. * NetworkCredential.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2014-2017 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. namespace WebSocketSharp.Net
  30. {
  31. /// <summary>
  32. /// Provides the credentials for the password-based authentication.
  33. /// </summary>
  34. public class NetworkCredential
  35. {
  36. #region Private Fields
  37. private string _domain;
  38. private static readonly string[] _noRoles;
  39. private string _password;
  40. private string[] _roles;
  41. private string _username;
  42. #endregion
  43. #region Static Constructor
  44. static NetworkCredential ()
  45. {
  46. _noRoles = new string[0];
  47. }
  48. #endregion
  49. #region Public Constructors
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="NetworkCredential"/> class with
  52. /// the specified <paramref name="username"/> and <paramref name="password"/>.
  53. /// </summary>
  54. /// <param name="username">
  55. /// A <see cref="string"/> that represents the username associated with
  56. /// the credentials.
  57. /// </param>
  58. /// <param name="password">
  59. /// A <see cref="string"/> that represents the password for the username
  60. /// associated with the credentials.
  61. /// </param>
  62. /// <exception cref="ArgumentNullException">
  63. /// <paramref name="username"/> is <see langword="null"/>.
  64. /// </exception>
  65. /// <exception cref="ArgumentException">
  66. /// <paramref name="username"/> is empty.
  67. /// </exception>
  68. public NetworkCredential (string username, string password)
  69. : this (username, password, null, null)
  70. {
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="NetworkCredential"/> class with
  74. /// the specified <paramref name="username"/>, <paramref name="password"/>,
  75. /// <paramref name="domain"/> and <paramref name="roles"/>.
  76. /// </summary>
  77. /// <param name="username">
  78. /// A <see cref="string"/> that represents the username associated with
  79. /// the credentials.
  80. /// </param>
  81. /// <param name="password">
  82. /// A <see cref="string"/> that represents the password for the username
  83. /// associated with the credentials.
  84. /// </param>
  85. /// <param name="domain">
  86. /// A <see cref="string"/> that represents the domain associated with
  87. /// the credentials.
  88. /// </param>
  89. /// <param name="roles">
  90. /// An array of <see cref="string"/> that represents the roles
  91. /// associated with the credentials if any.
  92. /// </param>
  93. /// <exception cref="ArgumentNullException">
  94. /// <paramref name="username"/> is <see langword="null"/>.
  95. /// </exception>
  96. /// <exception cref="ArgumentException">
  97. /// <paramref name="username"/> is empty.
  98. /// </exception>
  99. public NetworkCredential (
  100. string username, string password, string domain, params string[] roles
  101. )
  102. {
  103. if (username == null)
  104. throw new ArgumentNullException ("username");
  105. if (username.Length == 0)
  106. throw new ArgumentException ("An empty string.", "username");
  107. _username = username;
  108. _password = password;
  109. _domain = domain;
  110. _roles = roles;
  111. }
  112. #endregion
  113. #region Public Properties
  114. /// <summary>
  115. /// Gets the domain associated with the credentials.
  116. /// </summary>
  117. /// <remarks>
  118. /// This property returns an empty string if the domain was
  119. /// initialized with <see langword="null"/>.
  120. /// </remarks>
  121. /// <value>
  122. /// A <see cref="string"/> that represents the domain name
  123. /// to which the username belongs.
  124. /// </value>
  125. public string Domain {
  126. get {
  127. return _domain ?? String.Empty;
  128. }
  129. internal set {
  130. _domain = value;
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the password for the username associated with the credentials.
  135. /// </summary>
  136. /// <remarks>
  137. /// This property returns an empty string if the password was
  138. /// initialized with <see langword="null"/>.
  139. /// </remarks>
  140. /// <value>
  141. /// A <see cref="string"/> that represents the password.
  142. /// </value>
  143. public string Password {
  144. get {
  145. return _password ?? String.Empty;
  146. }
  147. internal set {
  148. _password = value;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets the roles associated with the credentials.
  153. /// </summary>
  154. /// <remarks>
  155. /// This property returns an empty array if the roles were
  156. /// initialized with <see langword="null"/>.
  157. /// </remarks>
  158. /// <value>
  159. /// An array of <see cref="string"/> that represents the role names
  160. /// to which the username belongs.
  161. /// </value>
  162. public string[] Roles {
  163. get {
  164. return _roles ?? _noRoles;
  165. }
  166. internal set {
  167. _roles = value;
  168. }
  169. }
  170. /// <summary>
  171. /// Gets the username associated with the credentials.
  172. /// </summary>
  173. /// <value>
  174. /// A <see cref="string"/> that represents the username.
  175. /// </value>
  176. public string Username {
  177. get {
  178. return _username;
  179. }
  180. internal set {
  181. _username = value;
  182. }
  183. }
  184. #endregion
  185. }
  186. }