UserNameServiceElement.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.IdentityModel.Selectors;
  10. using System.Runtime;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Activation;
  13. using System.ServiceModel.Security;
  14. public sealed partial class UserNameServiceElement : ConfigurationElement
  15. {
  16. public UserNameServiceElement()
  17. {
  18. }
  19. [ConfigurationProperty(ConfigurationStrings.UserNamePasswordValidationMode, DefaultValue = UserNamePasswordServiceCredential.DefaultUserNamePasswordValidationMode)]
  20. [ServiceModelEnumValidator(typeof(UserNamePasswordValidationModeHelper))]
  21. public UserNamePasswordValidationMode UserNamePasswordValidationMode
  22. {
  23. get { return (UserNamePasswordValidationMode)base[ConfigurationStrings.UserNamePasswordValidationMode]; }
  24. set { base[ConfigurationStrings.UserNamePasswordValidationMode] = value; }
  25. }
  26. [ConfigurationProperty(ConfigurationStrings.IncludeWindowsGroups, DefaultValue = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims)]
  27. public bool IncludeWindowsGroups
  28. {
  29. get { return (bool)base[ConfigurationStrings.IncludeWindowsGroups]; }
  30. set { base[ConfigurationStrings.IncludeWindowsGroups] = value; }
  31. }
  32. [ConfigurationProperty(ConfigurationStrings.MembershipProviderName, DefaultValue = "")]
  33. [StringValidator(MinLength = 0)]
  34. public string MembershipProviderName
  35. {
  36. get { return (string)base[ConfigurationStrings.MembershipProviderName]; }
  37. set
  38. {
  39. if (String.IsNullOrEmpty(value))
  40. {
  41. value = String.Empty;
  42. }
  43. base[ConfigurationStrings.MembershipProviderName] = value;
  44. }
  45. }
  46. [ConfigurationProperty(ConfigurationStrings.CustomUserNamePasswordValidatorType, DefaultValue = "")]
  47. [StringValidator(MinLength = 0)]
  48. public string CustomUserNamePasswordValidatorType
  49. {
  50. get { return (string)base[ConfigurationStrings.CustomUserNamePasswordValidatorType]; }
  51. set
  52. {
  53. if (String.IsNullOrEmpty(value))
  54. {
  55. value = String.Empty;
  56. }
  57. base[ConfigurationStrings.CustomUserNamePasswordValidatorType] = value;
  58. }
  59. }
  60. [ConfigurationProperty(ConfigurationStrings.CacheLogonTokens, DefaultValue = UserNamePasswordServiceCredential.DefaultCacheLogonTokens)]
  61. public bool CacheLogonTokens
  62. {
  63. get { return (bool)base[ConfigurationStrings.CacheLogonTokens]; }
  64. set { base[ConfigurationStrings.CacheLogonTokens] = value; }
  65. }
  66. [ConfigurationProperty(ConfigurationStrings.MaxCachedLogonTokens, DefaultValue = UserNamePasswordServiceCredential.DefaultMaxCachedLogonTokens)]
  67. [IntegerValidator(MinValue = 1)]
  68. public int MaxCachedLogonTokens
  69. {
  70. get { return (int)base[ConfigurationStrings.MaxCachedLogonTokens]; }
  71. set { base[ConfigurationStrings.MaxCachedLogonTokens] = value; }
  72. }
  73. [ConfigurationProperty(ConfigurationStrings.CachedLogonTokenLifetime, DefaultValue = UserNamePasswordServiceCredential.DefaultCachedLogonTokenLifetimeString)]
  74. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  75. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanOneTick)]
  76. public TimeSpan CachedLogonTokenLifetime
  77. {
  78. get { return (TimeSpan)base[ConfigurationStrings.CachedLogonTokenLifetime]; }
  79. set { base[ConfigurationStrings.CachedLogonTokenLifetime] = value; }
  80. }
  81. public void Copy(UserNameServiceElement from)
  82. {
  83. if (this.IsReadOnly())
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  86. }
  87. if (null == from)
  88. {
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
  90. }
  91. this.UserNamePasswordValidationMode = from.UserNamePasswordValidationMode;
  92. this.IncludeWindowsGroups = from.IncludeWindowsGroups;
  93. this.MembershipProviderName = from.MembershipProviderName;
  94. this.CustomUserNamePasswordValidatorType = from.CustomUserNamePasswordValidatorType;
  95. this.CacheLogonTokens = from.CacheLogonTokens;
  96. this.MaxCachedLogonTokens = from.MaxCachedLogonTokens;
  97. this.CachedLogonTokenLifetime = from.CachedLogonTokenLifetime;
  98. }
  99. internal void ApplyConfiguration(UserNamePasswordServiceCredential userName)
  100. {
  101. if (userName == null)
  102. {
  103. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("userName");
  104. }
  105. userName.UserNamePasswordValidationMode = this.UserNamePasswordValidationMode;
  106. userName.IncludeWindowsGroups = this.IncludeWindowsGroups;
  107. userName.CacheLogonTokens = this.CacheLogonTokens;
  108. userName.MaxCachedLogonTokens = this.MaxCachedLogonTokens;
  109. userName.CachedLogonTokenLifetime = this.CachedLogonTokenLifetime;
  110. PropertyInformationCollection propertyInfo = this.ElementInformation.Properties;
  111. if (propertyInfo[ConfigurationStrings.MembershipProviderName].ValueOrigin != PropertyValueOrigin.Default)
  112. {
  113. userName.MembershipProvider = SystemWebHelper.GetMembershipProvider(this.MembershipProviderName);
  114. if (userName.MembershipProvider == null)
  115. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.InvalidMembershipProviderSpecifiedInConfig, this.MembershipProviderName)));
  116. }
  117. else if (userName.UserNamePasswordValidationMode == UserNamePasswordValidationMode.MembershipProvider)
  118. {
  119. userName.MembershipProvider = SystemWebHelper.GetMembershipProvider();
  120. }
  121. if (!string.IsNullOrEmpty(this.CustomUserNamePasswordValidatorType))
  122. {
  123. Type validatorType = System.Type.GetType(this.CustomUserNamePasswordValidatorType, true);
  124. if (!typeof(UserNamePasswordValidator).IsAssignableFrom(validatorType))
  125. {
  126. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  127. SR.GetString(SR.ConfigInvalidUserNamePasswordValidatorType, this.CustomUserNamePasswordValidatorType, typeof(UserNamePasswordValidator).ToString())));
  128. }
  129. userName.CustomUserNamePasswordValidator = (UserNamePasswordValidator)Activator.CreateInstance(validatorType);
  130. }
  131. }
  132. }
  133. }