ServiceCredentialsElement.cs 8.2 KB

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