SecurityCookieModeValidator.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Description;
  9. using System.ServiceModel.Dispatcher;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Security.Tokens;
  15. class SecurityCookieModeValidator : IServiceBehavior
  16. {
  17. void CheckForCookie(SecurityTokenParameters tokenParameters, ServiceEndpoint endpoint)
  18. {
  19. bool cookie = false;
  20. SecureConversationSecurityTokenParameters sc = tokenParameters as SecureConversationSecurityTokenParameters;
  21. if (sc != null && sc.RequireCancellation == false)
  22. cookie = true;
  23. SspiSecurityTokenParameters sspi = tokenParameters as SspiSecurityTokenParameters;
  24. if (sspi != null && sspi.RequireCancellation == false)
  25. cookie = true;
  26. SspiSecurityTokenParameters ssl = tokenParameters as SspiSecurityTokenParameters;
  27. if (ssl != null && ssl.RequireCancellation == false)
  28. cookie = true;
  29. if (cookie)
  30. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.RequireNonCookieMode, endpoint.Binding.Name, endpoint.Binding.Namespace)));
  31. }
  32. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  33. {
  34. }
  35. void IServiceBehavior.Validate(ServiceDescription service, ServiceHostBase serviceHostBase)
  36. {
  37. }
  38. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription service, ServiceHostBase serviceHostBase)
  39. {
  40. // The philosophy here is to respect settings obtained from the
  41. // service surrogate class' attributes, as written by the user,
  42. // while rejecting those that contradict our requirements.
  43. // We never want to silently overwrite a user's attributes.
  44. // So we either accept overrides or reject them.
  45. //
  46. // If you're changing this code, you'll probably also want to change
  47. // ComPlusServiceLoader.AddBehaviors
  48. foreach (ServiceEndpoint endpoint in service.Endpoints)
  49. {
  50. ICollection<BindingElement> bindingElements = endpoint.Binding.CreateBindingElements();
  51. foreach (BindingElement element in bindingElements)
  52. {
  53. SymmetricSecurityBindingElement sbe = (element as SymmetricSecurityBindingElement);
  54. if (sbe != null)
  55. {
  56. this.CheckForCookie(sbe.ProtectionTokenParameters, endpoint);
  57. foreach (SecurityTokenParameters p in sbe.EndpointSupportingTokenParameters.Endorsing)
  58. this.CheckForCookie(p, endpoint);
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }