UserNamePasswordServiceCredential.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Globalization;
  7. using System.IdentityModel.Selectors;
  8. using System.Runtime;
  9. using System.Runtime.CompilerServices;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Activation;
  12. using System.Web.Security;
  13. public sealed class UserNamePasswordServiceCredential
  14. {
  15. internal const UserNamePasswordValidationMode DefaultUserNamePasswordValidationMode = UserNamePasswordValidationMode.Windows;
  16. internal const bool DefaultCacheLogonTokens = false;
  17. internal const int DefaultMaxCachedLogonTokens = 128;
  18. internal const string DefaultCachedLogonTokenLifetimeString = "00:15:00";
  19. internal static readonly TimeSpan DefaultCachedLogonTokenLifetime = TimeSpan.Parse(DefaultCachedLogonTokenLifetimeString, CultureInfo.InvariantCulture);
  20. UserNamePasswordValidationMode validationMode = DefaultUserNamePasswordValidationMode;
  21. UserNamePasswordValidator validator;
  22. object membershipProvider;
  23. bool includeWindowsGroups = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims;
  24. bool cacheLogonTokens = DefaultCacheLogonTokens;
  25. int maxCachedLogonTokens = DefaultMaxCachedLogonTokens;
  26. TimeSpan cachedLogonTokenLifetime = DefaultCachedLogonTokenLifetime;
  27. bool isReadOnly;
  28. internal UserNamePasswordServiceCredential()
  29. {
  30. // empty
  31. }
  32. internal UserNamePasswordServiceCredential(UserNamePasswordServiceCredential other)
  33. {
  34. this.includeWindowsGroups = other.includeWindowsGroups;
  35. this.membershipProvider = other.membershipProvider;
  36. this.validationMode = other.validationMode;
  37. this.validator = other.validator;
  38. this.cacheLogonTokens = other.cacheLogonTokens;
  39. this.maxCachedLogonTokens = other.maxCachedLogonTokens;
  40. this.cachedLogonTokenLifetime = other.cachedLogonTokenLifetime;
  41. this.isReadOnly = other.isReadOnly;
  42. }
  43. public UserNamePasswordValidationMode UserNamePasswordValidationMode
  44. {
  45. get
  46. {
  47. return this.validationMode;
  48. }
  49. set
  50. {
  51. UserNamePasswordValidationModeHelper.Validate(value);
  52. ThrowIfImmutable();
  53. this.validationMode = value;
  54. }
  55. }
  56. public UserNamePasswordValidator CustomUserNamePasswordValidator
  57. {
  58. get
  59. {
  60. return this.validator;
  61. }
  62. set
  63. {
  64. ThrowIfImmutable();
  65. this.validator = value;
  66. }
  67. }
  68. public MembershipProvider MembershipProvider
  69. {
  70. get
  71. {
  72. return (MembershipProvider)this.membershipProvider;
  73. }
  74. set
  75. {
  76. ThrowIfImmutable();
  77. this.membershipProvider = value;
  78. }
  79. }
  80. public bool IncludeWindowsGroups
  81. {
  82. get
  83. {
  84. return this.includeWindowsGroups;
  85. }
  86. set
  87. {
  88. ThrowIfImmutable();
  89. this.includeWindowsGroups = value;
  90. }
  91. }
  92. public bool CacheLogonTokens
  93. {
  94. get
  95. {
  96. return this.cacheLogonTokens;
  97. }
  98. set
  99. {
  100. ThrowIfImmutable();
  101. this.cacheLogonTokens = value;
  102. }
  103. }
  104. public int MaxCachedLogonTokens
  105. {
  106. get
  107. {
  108. return this.maxCachedLogonTokens;
  109. }
  110. set
  111. {
  112. if (value <= 0)
  113. {
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", SR.GetString(SR.ValueMustBeGreaterThanZero)));
  115. }
  116. ThrowIfImmutable();
  117. this.maxCachedLogonTokens = value;
  118. }
  119. }
  120. public TimeSpan CachedLogonTokenLifetime
  121. {
  122. get
  123. {
  124. return this.cachedLogonTokenLifetime;
  125. }
  126. set
  127. {
  128. if (value <= TimeSpan.Zero)
  129. {
  130. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", SR.GetString(SR.TimeSpanMustbeGreaterThanTimeSpanZero)));
  131. }
  132. if (TimeoutHelper.IsTooLarge(value))
  133. {
  134. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  135. SR.GetString(SR.SFxTimeoutOutOfRangeTooBig)));
  136. }
  137. ThrowIfImmutable();
  138. this.cachedLogonTokenLifetime = value;
  139. }
  140. }
  141. internal UserNamePasswordValidator GetUserNamePasswordValidator()
  142. {
  143. if (this.validationMode == UserNamePasswordValidationMode.MembershipProvider)
  144. {
  145. return this.GetMembershipProviderValidator();
  146. }
  147. else if (this.validationMode == UserNamePasswordValidationMode.Custom)
  148. {
  149. if (this.validator == null)
  150. {
  151. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MissingCustomUserNamePasswordValidator)));
  152. }
  153. return this.validator;
  154. }
  155. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  156. }
  157. [MethodImpl(MethodImplOptions.NoInlining)]
  158. UserNamePasswordValidator GetMembershipProviderValidator()
  159. {
  160. MembershipProvider provider;
  161. if (this.membershipProvider != null)
  162. {
  163. provider = (MembershipProvider)this.membershipProvider;
  164. }
  165. else
  166. {
  167. provider = SystemWebHelper.GetMembershipProvider();
  168. }
  169. if (provider == null)
  170. {
  171. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MissingMembershipProvider)));
  172. }
  173. return UserNamePasswordValidator.CreateMembershipProviderValidator(provider);
  174. }
  175. internal void MakeReadOnly()
  176. {
  177. this.isReadOnly = true;
  178. }
  179. void ThrowIfImmutable()
  180. {
  181. if (this.isReadOnly)
  182. {
  183. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  184. }
  185. }
  186. }
  187. }