ClientCredentialsElement.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Configuration;
  7. using System.ServiceModel;
  8. using System.Globalization;
  9. using System.Net.Security;
  10. using System.ServiceModel.Description;
  11. using System.ServiceModel.Security;
  12. using System.Security.Cryptography.X509Certificates;
  13. using System.Reflection;
  14. using System.Runtime.InteropServices;
  15. public partial class ClientCredentialsElement : BehaviorExtensionElement
  16. {
  17. public ClientCredentialsElement()
  18. {
  19. }
  20. [ConfigurationProperty(ConfigurationStrings.Type, DefaultValue = "")]
  21. [StringValidator(MinLength = 0)]
  22. public string Type
  23. {
  24. get { return (string)base[ConfigurationStrings.Type]; }
  25. set
  26. {
  27. if (String.IsNullOrEmpty(value))
  28. {
  29. value = String.Empty;
  30. }
  31. base[ConfigurationStrings.Type] = value;
  32. }
  33. }
  34. [ConfigurationProperty(ConfigurationStrings.UseIdentityConfiguration, DefaultValue = false)]
  35. public bool UseIdentityConfiguration
  36. {
  37. get
  38. {
  39. return (bool)base[ConfigurationStrings.UseIdentityConfiguration];
  40. }
  41. set
  42. {
  43. base[ConfigurationStrings.UseIdentityConfiguration] = value;
  44. }
  45. }
  46. [ConfigurationProperty(ConfigurationStrings.ClientCertificate)]
  47. public X509InitiatorCertificateClientElement ClientCertificate
  48. {
  49. get { return (X509InitiatorCertificateClientElement)base[ConfigurationStrings.ClientCertificate]; }
  50. }
  51. [ConfigurationProperty(ConfigurationStrings.ServiceCertificate)]
  52. public X509RecipientCertificateClientElement ServiceCertificate
  53. {
  54. get { return (X509RecipientCertificateClientElement)base[ConfigurationStrings.ServiceCertificate]; }
  55. }
  56. [ConfigurationProperty(ConfigurationStrings.Windows)]
  57. public WindowsClientElement Windows
  58. {
  59. get { return (WindowsClientElement)base[ConfigurationStrings.Windows]; }
  60. }
  61. [ConfigurationProperty(ConfigurationStrings.IssuedToken)]
  62. public IssuedTokenClientElement IssuedToken
  63. {
  64. get { return (IssuedTokenClientElement)base[ConfigurationStrings.IssuedToken]; }
  65. }
  66. [ConfigurationProperty(ConfigurationStrings.HttpDigest)]
  67. public HttpDigestClientElement HttpDigest
  68. {
  69. get { return (HttpDigestClientElement)base[ConfigurationStrings.HttpDigest]; }
  70. }
  71. [ConfigurationProperty(ConfigurationStrings.Peer)]
  72. public PeerCredentialElement Peer
  73. {
  74. get { return (PeerCredentialElement)base[ConfigurationStrings.Peer]; }
  75. }
  76. [ConfigurationProperty(ConfigurationStrings.SupportInteractive, DefaultValue = ClientCredentials.SupportInteractiveDefault)]
  77. public bool SupportInteractive
  78. {
  79. get { return (bool)base[ConfigurationStrings.SupportInteractive]; }
  80. set { base[ConfigurationStrings.SupportInteractive] = value; }
  81. }
  82. public override void CopyFrom(ServiceModelExtensionElement from)
  83. {
  84. base.CopyFrom(from);
  85. ClientCredentialsElement source = (ClientCredentialsElement)from;
  86. #pragma warning suppress 56506 //[....]; base.CopyFrom() checks for 'from' being null
  87. this.ClientCertificate.Copy(source.ClientCertificate);
  88. this.ServiceCertificate.Copy(source.ServiceCertificate);
  89. this.Windows.Copy(source.Windows);
  90. this.IssuedToken.Copy(source.IssuedToken);
  91. this.HttpDigest.Copy(source.HttpDigest);
  92. this.Peer.Copy(source.Peer);
  93. this.SupportInteractive = source.SupportInteractive;
  94. this.Type = source.Type;
  95. this.UseIdentityConfiguration = source.UseIdentityConfiguration;
  96. }
  97. protected internal override object CreateBehavior()
  98. {
  99. ClientCredentials behavior;
  100. if (string.IsNullOrEmpty(this.Type))
  101. {
  102. behavior = new ClientCredentials();
  103. }
  104. else
  105. {
  106. Type credentialsType = System.Type.GetType(this.Type, true);
  107. if (!typeof(ClientCredentials).IsAssignableFrom(credentialsType))
  108. {
  109. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  110. SR.GetString(SR.ConfigInvalidClientCredentialsType, this.Type, credentialsType.AssemblyQualifiedName)));
  111. }
  112. behavior = (ClientCredentials) Activator.CreateInstance(credentialsType);
  113. }
  114. ApplyConfiguration(behavior);
  115. return behavior;
  116. }
  117. protected internal void ApplyConfiguration( ClientCredentials behavior )
  118. {
  119. if (null == behavior)
  120. {
  121. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("behavior");
  122. }
  123. PropertyInformationCollection propertyInfo = this.ElementInformation.Properties;
  124. if (propertyInfo[ConfigurationStrings.Windows].ValueOrigin != PropertyValueOrigin.Default)
  125. {
  126. this.Windows.ApplyConfiguration(behavior.Windows);
  127. }
  128. if (propertyInfo[ConfigurationStrings.ClientCertificate].ValueOrigin != PropertyValueOrigin.Default)
  129. {
  130. this.ClientCertificate.ApplyConfiguration(behavior.ClientCertificate);
  131. }
  132. if (propertyInfo[ConfigurationStrings.ServiceCertificate].ValueOrigin != PropertyValueOrigin.Default)
  133. {
  134. this.ServiceCertificate.ApplyConfiguration(behavior.ServiceCertificate);
  135. }
  136. if (propertyInfo[ConfigurationStrings.IssuedToken].ValueOrigin != PropertyValueOrigin.Default)
  137. {
  138. this.IssuedToken.ApplyConfiguration(behavior.IssuedToken);
  139. }
  140. if (propertyInfo[ConfigurationStrings.HttpDigest].ValueOrigin != PropertyValueOrigin.Default)
  141. {
  142. this.HttpDigest.ApplyConfiguration(behavior.HttpDigest);
  143. }
  144. if (propertyInfo[ConfigurationStrings.Peer].ValueOrigin != PropertyValueOrigin.Default)
  145. {
  146. this.Peer.ApplyConfiguration(behavior.Peer);
  147. }
  148. if ( propertyInfo[ConfigurationStrings.UseIdentityConfiguration].ValueOrigin != PropertyValueOrigin.Default )
  149. {
  150. behavior.UseIdentityConfiguration = this.UseIdentityConfiguration;
  151. }
  152. behavior.SupportInteractive = this.SupportInteractive;
  153. }
  154. public override Type BehaviorType
  155. {
  156. get { return typeof(ClientCredentials); }
  157. }
  158. }
  159. }