IssuedTokenServiceElement.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. using System.IdentityModel.Selectors;
  9. using System.IdentityModel.Tokens;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.ServiceModel;
  12. using System.ServiceModel.Security;
  13. using System.Xml;
  14. public sealed partial class IssuedTokenServiceElement : ConfigurationElement
  15. {
  16. public IssuedTokenServiceElement()
  17. {
  18. }
  19. [ConfigurationProperty(ConfigurationStrings.AllowedAudienceUris)]
  20. public AllowedAudienceUriElementCollection AllowedAudienceUris
  21. {
  22. get { return (AllowedAudienceUriElementCollection)base[ConfigurationStrings.AllowedAudienceUris]; }
  23. }
  24. [ConfigurationProperty(ConfigurationStrings.AudienceUriMode, DefaultValue = IssuedTokenServiceCredential.DefaultAudienceUriMode)]
  25. [ServiceModelEnumValidator(typeof(AudienceUriModeValidationHelper))]
  26. public AudienceUriMode AudienceUriMode
  27. {
  28. get { return (AudienceUriMode)base[ConfigurationStrings.AudienceUriMode]; }
  29. set { base[ConfigurationStrings.AudienceUriMode] = value; }
  30. }
  31. [ConfigurationProperty(ConfigurationStrings.CustomCertificateValidatorType, DefaultValue = "")]
  32. [StringValidator(MinLength = 0)]
  33. public string CustomCertificateValidatorType
  34. {
  35. get { return (string)base[ConfigurationStrings.CustomCertificateValidatorType]; }
  36. set
  37. {
  38. if (String.IsNullOrEmpty(value))
  39. {
  40. value = String.Empty;
  41. }
  42. base[ConfigurationStrings.CustomCertificateValidatorType] = value;
  43. }
  44. }
  45. [ConfigurationProperty(ConfigurationStrings.CertificateValidationMode, DefaultValue = IssuedTokenServiceCredential.DefaultCertificateValidationMode)]
  46. [ServiceModelEnumValidator(typeof(X509CertificateValidationModeHelper))]
  47. public X509CertificateValidationMode CertificateValidationMode
  48. {
  49. get { return (X509CertificateValidationMode)base[ConfigurationStrings.CertificateValidationMode]; }
  50. set { base[ConfigurationStrings.CertificateValidationMode] = value; }
  51. }
  52. [ConfigurationProperty(ConfigurationStrings.RevocationMode, DefaultValue = IssuedTokenServiceCredential.DefaultRevocationMode)]
  53. [StandardRuntimeEnumValidator(typeof(X509RevocationMode))]
  54. public X509RevocationMode RevocationMode
  55. {
  56. get { return (X509RevocationMode)base[ConfigurationStrings.RevocationMode]; }
  57. set { base[ConfigurationStrings.RevocationMode] = value; }
  58. }
  59. [ConfigurationProperty(ConfigurationStrings.TrustedStoreLocation, DefaultValue = IssuedTokenServiceCredential.DefaultTrustedStoreLocation)]
  60. [StandardRuntimeEnumValidator(typeof(StoreLocation))]
  61. public StoreLocation TrustedStoreLocation
  62. {
  63. get { return (StoreLocation)base[ConfigurationStrings.TrustedStoreLocation]; }
  64. set { base[ConfigurationStrings.TrustedStoreLocation] = value; }
  65. }
  66. [ConfigurationProperty(ConfigurationStrings.SamlSerializerType, DefaultValue = "")]
  67. [StringValidator(MinLength = 0)]
  68. public string SamlSerializerType
  69. {
  70. get { return (string)base[ConfigurationStrings.SamlSerializerType]; }
  71. set
  72. {
  73. if (String.IsNullOrEmpty(value))
  74. {
  75. value = String.Empty;
  76. }
  77. base[ConfigurationStrings.SamlSerializerType] = value;
  78. }
  79. }
  80. [ConfigurationProperty(ConfigurationStrings.KnownCertificates)]
  81. public X509CertificateTrustedIssuerElementCollection KnownCertificates
  82. {
  83. get { return (X509CertificateTrustedIssuerElementCollection)base[ConfigurationStrings.KnownCertificates]; }
  84. }
  85. [ConfigurationProperty(ConfigurationStrings.AllowUntrustedRsaIssuers, DefaultValue = IssuedTokenServiceCredential.DefaultAllowUntrustedRsaIssuers)]
  86. public bool AllowUntrustedRsaIssuers
  87. {
  88. get { return (bool)base[ConfigurationStrings.AllowUntrustedRsaIssuers]; }
  89. set { base[ConfigurationStrings.AllowUntrustedRsaIssuers] = value; }
  90. }
  91. public void Copy(IssuedTokenServiceElement from)
  92. {
  93. if (this.IsReadOnly())
  94. {
  95. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  96. }
  97. if (null == from)
  98. {
  99. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("from");
  100. }
  101. this.SamlSerializerType = from.SamlSerializerType;
  102. #pragma warning suppress 56506 // [....]; ElementInformation is never null.
  103. PropertyInformationCollection propertyInfo = from.ElementInformation.Properties;
  104. if (propertyInfo[ConfigurationStrings.KnownCertificates].ValueOrigin != PropertyValueOrigin.Default)
  105. {
  106. this.KnownCertificates.Clear();
  107. foreach (X509CertificateTrustedIssuerElement src in from.KnownCertificates)
  108. {
  109. X509CertificateTrustedIssuerElement copy = new X509CertificateTrustedIssuerElement();
  110. copy.Copy(src);
  111. this.KnownCertificates.Add(copy);
  112. }
  113. }
  114. if (propertyInfo[ConfigurationStrings.AllowedAudienceUris].ValueOrigin != PropertyValueOrigin.Default)
  115. {
  116. this.AllowedAudienceUris.Clear();
  117. foreach (AllowedAudienceUriElement src in from.AllowedAudienceUris)
  118. {
  119. AllowedAudienceUriElement copy = new AllowedAudienceUriElement();
  120. copy.AllowedAudienceUri = src.AllowedAudienceUri;
  121. this.AllowedAudienceUris.Add(copy);
  122. }
  123. }
  124. this.AllowUntrustedRsaIssuers = from.AllowUntrustedRsaIssuers;
  125. this.CertificateValidationMode = from.CertificateValidationMode;
  126. this.AudienceUriMode = from.AudienceUriMode;
  127. this.CustomCertificateValidatorType = from.CustomCertificateValidatorType;
  128. this.RevocationMode = from.RevocationMode;
  129. this.TrustedStoreLocation = from.TrustedStoreLocation;
  130. }
  131. internal void ApplyConfiguration(IssuedTokenServiceCredential issuedToken)
  132. {
  133. if (issuedToken == null)
  134. {
  135. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("issuedToken");
  136. }
  137. issuedToken.CertificateValidationMode = this.CertificateValidationMode;
  138. issuedToken.RevocationMode = this.RevocationMode;
  139. issuedToken.TrustedStoreLocation = this.TrustedStoreLocation;
  140. issuedToken.AudienceUriMode = this.AudienceUriMode;
  141. if (!string.IsNullOrEmpty(this.CustomCertificateValidatorType))
  142. {
  143. Type type = System.Type.GetType(this.CustomCertificateValidatorType, true);
  144. if (!typeof(X509CertificateValidator).IsAssignableFrom(type))
  145. {
  146. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  147. SR.GetString(SR.ConfigInvalidCertificateValidatorType, this.CustomCertificateValidatorType, typeof(X509CertificateValidator).ToString())));
  148. }
  149. issuedToken.CustomCertificateValidator = (X509CertificateValidator)Activator.CreateInstance(type);
  150. }
  151. if (!string.IsNullOrEmpty(this.SamlSerializerType))
  152. {
  153. Type type = System.Type.GetType(this.SamlSerializerType, true);
  154. if (!typeof(SamlSerializer).IsAssignableFrom(type))
  155. {
  156. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  157. SR.GetString(SR.ConfigInvalidSamlSerializerType, this.SamlSerializerType, typeof(SamlSerializer).ToString())));
  158. }
  159. issuedToken.SamlSerializer = (SamlSerializer)Activator.CreateInstance(type);
  160. }
  161. PropertyInformationCollection propertyInfo = this.ElementInformation.Properties;
  162. if (propertyInfo[ConfigurationStrings.KnownCertificates].ValueOrigin != PropertyValueOrigin.Default)
  163. {
  164. foreach (X509CertificateTrustedIssuerElement src in this.KnownCertificates)
  165. {
  166. issuedToken.KnownCertificates.Add(SecurityUtils.GetCertificateFromStore(src.StoreName, src.StoreLocation, src.X509FindType, src.FindValue, null));
  167. }
  168. }
  169. if (propertyInfo[ConfigurationStrings.AllowedAudienceUris].ValueOrigin != PropertyValueOrigin.Default)
  170. {
  171. foreach (AllowedAudienceUriElement src in this.AllowedAudienceUris)
  172. {
  173. issuedToken.AllowedAudienceUris.Add(src.AllowedAudienceUri);
  174. }
  175. }
  176. issuedToken.AllowUntrustedRsaIssuers = this.AllowUntrustedRsaIssuers;
  177. }
  178. }
  179. }