SecurityHandler.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel.Channels;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Security.Tokens;
  7. namespace System.ServiceModel.Dispatcher
  8. {
  9. internal class SecurityHandler : BaseRequestProcessorHandler
  10. {
  11. protected override bool ProcessRequest (MessageProcessingContext mrc)
  12. {
  13. DispatchRuntime dispatch_runtime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
  14. // FIXME: I doubt this should be done at this "handler"
  15. // layer, especially considering about non-ServiceHost
  16. // use of SecurityBindingElement + listener.
  17. //
  18. // For example there is no way to handle it in duplex
  19. // dispatch callbacks.
  20. if (dispatch_runtime.ChannelDispatcher == null)
  21. return false;
  22. Message negoResponce = null;
  23. // process WS-Trust based negotiation
  24. MessageSecurityBindingSupport support =
  25. dispatch_runtime.ChannelDispatcher.Listener.GetProperty<MessageSecurityBindingSupport> ();
  26. if (support != null && mrc.IncomingMessage.Headers.FindHeader ("Security", Constants.WssNamespace) < 0) {
  27. CommunicationSecurityTokenAuthenticator nego =
  28. support.TokenAuthenticator as CommunicationSecurityTokenAuthenticator;
  29. if (nego != null)
  30. negoResponce = nego.Communication.ProcessNegotiation (mrc.IncomingMessage);
  31. }
  32. if (negoResponce == null)
  33. return false;
  34. ReplyNegoResponse (mrc, negoResponce);
  35. return true;
  36. }
  37. void ReplyNegoResponse (MessageProcessingContext mrc, Message negoResponse)
  38. {
  39. negoResponse.Headers.CopyHeadersFrom (mrc.OperationContext.OutgoingMessageHeaders);
  40. negoResponse.Properties.CopyProperties (mrc.OperationContext.OutgoingMessageProperties);
  41. mrc.RequestContext.Reply (negoResponse, mrc.Operation.Parent.ChannelDispatcher.timeouts.SendTimeout);
  42. return;
  43. }
  44. }
  45. }