/WCFWebApi/src/Microsoft.ServiceModel.Internal/Microsoft/ServiceModel/HttpTransportSecurityExtensionMethods.cs

# · C# · 59 lines · 46 code · 10 blank · 3 comment · 11 complexity · fb521e68fe293730042442d63b0b8c80 MD5 · raw file

  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace Microsoft.ServiceModel
  5. {
  6. using System;
  7. using System.Net;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using Microsoft.Server.Common;
  11. internal static class HttpTransportSecurityExtensionMethods
  12. {
  13. internal static void ConfigureTransportProtectionAndAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpsTransportBindingElement httpsTransportBindingElement)
  14. {
  15. Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
  16. Fx.Assert(httpsTransportBindingElement != null, "httpsTransportBindingElement cannot be null");
  17. httpTransportSecurity.ConfigureAuthentication(httpsTransportBindingElement);
  18. httpsTransportBindingElement.RequireClientCertificate = httpTransportSecurity.ClientCredentialType == HttpClientCredentialType.Certificate;
  19. }
  20. internal static void ConfigureTransportAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
  21. {
  22. Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
  23. Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
  24. if (httpTransportSecurity.ClientCredentialType == HttpClientCredentialType.Certificate)
  25. {
  26. throw Fx.Exception.AsError(new InvalidOperationException(SR.CertificateUnsupportedForHttpTransportCredentialOnly));
  27. }
  28. httpTransportSecurity.ConfigureAuthentication(httpTransportBindingElement);
  29. }
  30. internal static void DisableTransportAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
  31. {
  32. Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
  33. Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
  34. httpTransportBindingElement.AuthenticationScheme = AuthenticationSchemes.Anonymous;
  35. httpTransportBindingElement.ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous;
  36. httpTransportBindingElement.Realm = string.Empty;
  37. httpTransportBindingElement.ExtendedProtectionPolicy = httpTransportSecurity.ExtendedProtectionPolicy;
  38. }
  39. private static void ConfigureAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
  40. {
  41. Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
  42. Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
  43. httpTransportBindingElement.AuthenticationScheme = HttpClientCredentialTypeHelper.MapToAuthenticationScheme(httpTransportSecurity.ClientCredentialType);
  44. httpTransportBindingElement.ProxyAuthenticationScheme = HttpProxyCredentialTypeHelper.MapToAuthenticationScheme(httpTransportSecurity.ProxyCredentialType);
  45. httpTransportBindingElement.Realm = httpTransportSecurity.Realm;
  46. httpTransportBindingElement.ExtendedProtectionPolicy = httpTransportSecurity.ExtendedProtectionPolicy;
  47. }
  48. }
  49. }