SecurityHandler.cs 1.8 KB

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