1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//
// BasicHttpBinding.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Xml;
using System.ServiceModel.Configuration;
namespace System.ServiceModel
{
public class BasicHttpBinding : Binding,
IBindingRuntimePreferences
{
bool allow_cookies, bypass_proxy_on_local;
HostNameComparisonMode host_name_comparison_mode
= HostNameComparisonMode.StrongWildcard;
long max_buffer_pool_size = 0x80000;
int max_buffer_size = 0x10000;
long max_recv_message_size = 0x10000;
WSMessageEncoding message_encoding
= WSMessageEncoding.Text;
Uri proxy_address;
XmlDictionaryReaderQuotas reader_quotas
= new XmlDictionaryReaderQuotas ();
EnvelopeVersion env_version = EnvelopeVersion.Soap11;
Encoding text_encoding = new UTF8Encoding ();
TransferMode transfer_mode
= TransferMode.Buffered;
bool use_default_web_proxy = true;
BasicHttpSecurity security;
public BasicHttpBinding ()
: this (BasicHttpSecurityMode.None)
{
}
#if !NET_2_1
public BasicHttpBinding (string configurationName)
: this ()
{
BindingsSection bindingsSection = ConfigUtil.BindingsSection;
BasicHttpBindingElement el =
bindingsSection.BasicHttpBinding.Bindings [configurationName];
el.ApplyConfiguration (this);
}
#endif
public BasicHttpBinding (
BasicHttpSecurityMode securityMode)
{
security = new BasicHttpSecurity (securityMode);
}
public bool AllowCookies {
get { return allow_cookies; }
set { allow_cookies = value; }
}
public bool BypassProxyOnLocal {
get { return bypass_proxy_on_local; }
set { bypass_proxy_on_local = value; }
}
#if NET_2_1
public bool EnableHttpCookieContainer { get; set; }
#endif
public HostNameComparisonMode HostNameComparisonMode {
get { return host_name_comparison_mode; }
set { host_name_comparison_mode = value; }
}
public long MaxBufferPoolSize {
get { return max_buffer_pool_size; }
set {
if (value <= 0)
throw new ArgumentOutOfRangeException ();
max_buffer_pool_size = value;
}
}
public int MaxBufferSize {
get { return max_buffer_size; }
set {
if (value <= 0)
throw new ArgumentOutOfRangeException ();
max_buffer_size = value;
}
}
public long MaxReceivedMessageSize {
get { return max_recv_message_size; }
set {
if (value <= 0)
throw new ArgumentOutOfRangeException ();
max_recv_message_size = value;
}
}
public WSMessageEncoding MessageEncoding {
get { return message_encoding; }
set { message_encoding = value; }
}
public Uri ProxyAddress {
get { return proxy_address; }
set { proxy_address = value; }
}
public XmlDictionaryReaderQuotas ReaderQuotas {
get { return reader_quotas; }
set { reader_quotas = value; }
}
public override string Scheme {
get {
switch (Security.Mode) {
case BasicHttpSecurityMode.Transport:
case BasicHttpSecurityMode.TransportWithMessageCredential:
return Uri.UriSchemeHttps;
default:
return Uri.UriSchemeHttp;
}
}
}
public BasicHttpSecurity Security {
get { return security; }
}
public EnvelopeVersion EnvelopeVersion {
get { return env_version; }
}
public Encoding TextEncoding {
get { return text_encoding; }
set { text_encoding = value; }
}
public TransferMode TransferMode {
get { return transfer_mode; }
set { transfer_mode = value; }
}
public bool UseDefaultWebProxy {
get { return use_default_web_proxy; }
set { use_default_web_proxy = value; }
}
public override BindingElementCollection
CreateBindingElements ()
{
var list = new List<BindingElement> ();
var security = CreateSecurityBindingElement ();
if (security != null)
list.Add (security);
#if NET_2_1
if (EnableHttpCookieContainer)
list.Add (new HttpCookieContainerBindingElement ());
#endif
list.Add (BuildMessageEncodingBindingElement ());
list.Add (GetTransport ());
return new BindingElementCollection (list.ToArray ());
}
SecurityBindingElement CreateSecurityBindingElement ()
{
SecurityBindingElement element;
switch (Security.Mode) {
#if !NET_2_1
case BasicHttpSecurityMode.Message:
if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
element = SecurityBindingElement.CreateMutualCertificateBindingElement (
MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
break;
case BasicHttpSecurityMode.TransportWithMessageCredential:
if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
// FIXME: pass proper security token parameters.
element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
else
element = new AsymmetricSecurityBindingElement ();
break;
#endif
default:
return null;
}
#if !NET_2_1
element.SetKeyDerivation (false);
element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
#endif
return element;
}
MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
{
if (MessageEncoding == WSMessageEncoding.Text) {
TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
#if !NET_2_1
ReaderQuotas.CopyTo (tm.ReaderQuotas);
#endif
return tm;
}
else
#if NET_2_1
throw new SystemException ("INTERNAL ERROR: should not happen");
#else
return new MtomMessageEncodingBindingElement (
MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
#endif
}
TransportBindingElement GetTransport ()
{
HttpTransportBindingElement h;
switch (Security.Mode) {
case BasicHttpSecurityMode.Transport:
case BasicHttpSecurityMode.TransportWithMessageCredential:
h = new HttpsTransportBindingElement ();
break;
default:
h = new HttpTransportBindingElement ();
break;
}
h.AllowCookies = AllowCookies;
h.BypassProxyOnLocal = BypassProxyOnLocal;
h.HostNameComparisonMode = HostNameComparisonMode;
h.MaxBufferPoolSize = MaxBufferPoolSize;
h.MaxBufferSize = MaxBufferSize;
h.MaxReceivedMessageSize = MaxReceivedMessageSize;
h.ProxyAddress = ProxyAddress;
h.UseDefaultWebProxy = UseDefaultWebProxy;
h.TransferMode = TransferMode;
#if NET_4_0
h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
#endif
#if !NET_2_1 || MOBILE
switch (Security.Transport.ClientCredentialType) {
case HttpClientCredentialType.Basic:
h.AuthenticationScheme = AuthenticationSchemes.Basic;
break;
case HttpClientCredentialType.Ntlm:
h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
break;
case HttpClientCredentialType.Windows:
h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
break;
case HttpClientCredentialType.Digest:
h.AuthenticationScheme = AuthenticationSchemes.Digest;
break;
case HttpClientCredentialType.Certificate:
switch (Security.Mode) {
case BasicHttpSecurityMode.Transport:
(h as HttpsTransportBindingElement).RequireClientCertificate = true;
break;
case BasicHttpSecurityMode.TransportCredentialOnly:
throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
}
break;
}
#endif
return h;
}
// explicit interface implementations
bool IBindingRuntimePreferences.ReceiveSynchronously {
get { return false; }
}
}
}
|