CallbackContextMessageHeader.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Security;
  10. using System.Xml;
  11. using System.Net.Security;
  12. class CallbackContextMessageHeader : MessageHeader
  13. {
  14. public const string CallbackContextHeaderName = "CallbackContext";
  15. public const string CallbackContextHeaderNamespace = "http://schemas.microsoft.com/ws/2008/02/context";
  16. public const string CallbackEndpointReference = "CallbackEndpointReference";
  17. static ChannelProtectionRequirements encryptAndSignChannelProtectionRequirements;
  18. static ChannelProtectionRequirements signChannelProtectionRequirements;
  19. EndpointAddress callbackAddress;
  20. AddressingVersion version;
  21. public CallbackContextMessageHeader(EndpointAddress callbackAddress, AddressingVersion version)
  22. : base()
  23. {
  24. if (callbackAddress == null)
  25. {
  26. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackAddress");
  27. }
  28. if (version == null)
  29. {
  30. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("version");
  31. }
  32. if (version != AddressingVersion.WSAddressing10)
  33. {
  34. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CallbackContextOnlySupportedInWSAddressing10, version)));
  35. }
  36. this.callbackAddress = callbackAddress;
  37. this.version = version;
  38. }
  39. public override string Name
  40. {
  41. get { return CallbackContextHeaderName; }
  42. }
  43. public override string Namespace
  44. {
  45. get { return CallbackContextHeaderNamespace; }
  46. }
  47. internal static ChannelProtectionRequirements GetChannelProtectionRequirements(ProtectionLevel protectionLevel)
  48. {
  49. ChannelProtectionRequirements result;
  50. if (protectionLevel == ProtectionLevel.EncryptAndSign)
  51. {
  52. if (encryptAndSignChannelProtectionRequirements == null)
  53. {
  54. MessagePartSpecification header = new MessagePartSpecification();
  55. header.HeaderTypes.Add(new XmlQualifiedName(CallbackContextHeaderName, CallbackContextHeaderNamespace));
  56. ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();
  57. requirements.IncomingSignatureParts.AddParts(header);
  58. requirements.IncomingEncryptionParts.AddParts(header);
  59. requirements.OutgoingSignatureParts.AddParts(header);
  60. requirements.OutgoingEncryptionParts.AddParts(header);
  61. requirements.MakeReadOnly();
  62. encryptAndSignChannelProtectionRequirements = requirements;
  63. }
  64. result = encryptAndSignChannelProtectionRequirements;
  65. }
  66. else if (protectionLevel == ProtectionLevel.Sign)
  67. {
  68. if (signChannelProtectionRequirements == null)
  69. {
  70. MessagePartSpecification header = new MessagePartSpecification();
  71. header.HeaderTypes.Add(new XmlQualifiedName(CallbackContextHeaderName, CallbackContextHeaderNamespace));
  72. ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();
  73. requirements.IncomingSignatureParts.AddParts(header);
  74. requirements.OutgoingSignatureParts.AddParts(header);
  75. requirements.MakeReadOnly();
  76. signChannelProtectionRequirements = requirements;
  77. }
  78. result = signChannelProtectionRequirements;
  79. }
  80. else
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("protectionLevel"));
  83. }
  84. return result;
  85. }
  86. internal static CallbackContextMessageProperty ParseCallbackContextHeader(XmlReader reader, AddressingVersion version)
  87. {
  88. if (reader == null)
  89. {
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  91. }
  92. if (version != AddressingVersion.WSAddressing10)
  93. {
  94. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.CallbackContextOnlySupportedInWSAddressing10, version)));
  95. }
  96. try
  97. {
  98. reader.ReadStartElement(CallbackContextHeaderName, CallbackContextHeaderNamespace);
  99. EndpointAddress callbackAddress = EndpointAddress.ReadFrom(version, reader, CallbackEndpointReference, CallbackContextHeaderNamespace);
  100. reader.ReadEndElement();
  101. return new CallbackContextMessageProperty(callbackAddress);
  102. }
  103. catch (XmlException e)
  104. {
  105. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  106. new ProtocolException(SR.GetString(SR.XmlFormatViolationInCallbackContextHeader), e));
  107. }
  108. }
  109. protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
  110. {
  111. if (writer == null)
  112. {
  113. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
  114. }
  115. this.callbackAddress.WriteTo(this.version, writer, CallbackEndpointReference, CallbackContextHeaderNamespace);
  116. }
  117. }
  118. }