2

I have a Attribute implementing IServiceBehavior to secure my WCF services, like below:

public class AuthorizedServiceAttribute : Attribute, IServiceBehavior
{
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        var token = string.Empty; // to do: get the token from message headers

        foreach (var operation in serviceHostBase.Description.Endpoints
            .SelectMany(endpoint => endpoint.Contract.Operations))
        {
            operation.Behaviors.Add(new AuthorizedMethodAttribute { Token = token });
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    { }

    #endregion
}

The problem is in order to get the message headers, I have to get the current OperationContext but I don't know how to to do it inside the ApplyDispatchBehavior. If I do it in the methods under secure, it works.

Lewis LE
  • 45
  • 5
  • You can have access to the property from IDispatchMessageInspector or IParameterInspector implementation. What is your goal? – Igor Tkachenko Apr 15 '14 at 13:15
  • My goal is: from the client I will attach a token into the message header and then get that token from server via attributes to validate the client – Lewis LE Apr 15 '14 at 14:00
  • @Legart: Thank you so much! I did succeed with the IParameterInspector – Lewis LE Apr 16 '14 at 02:47

1 Answers1

0

In that case I would suggest to use Message contract: Message Contract

Or you can also use IDispatchMessageInspector: Message Inspector, or IParameterInspector as I mentioned in comments.

Community
  • 1
  • 1
Igor Tkachenko
  • 1,120
  • 7
  • 17