When and where to set a custom IOperationInvoker?

前端 未结 3 1101
忘掉有多难
忘掉有多难 2020-12-10 05:00

I\'m trying to extend WCF so that I can have a RESTful web service, in which, for each operation, I perform a verification of the HTTP Authorization header, whose value I us

相关标签:
3条回答
  • 2020-12-10 05:41

    Instead of setting invokers at ApplyDispatchBehavior() method, you have to make an IOperationBehavior implementor:

     public class MyOperationBehavior: IOperationBehavior
     {
      public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
      {
      }
    
      public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
      {
      }
    
      public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
      {
       dispatchOperation.Invoker = new BookSmarTkOperationInvoker(dispatchOperation.Invoker);
      }
    
      public void Validate(OperationDescription operationDescription)
      {
      }
     }
    

    and then at ApplyDispatchBehavior() you should set that behavior:

      public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
      {
        foreach (var operation in endpoint.Contract.Operations) {
          if (operation.Behaviors.Contains(typeof(MyOperationBehavior)))
           continue;
    
          operation.Behaviors.Add(new MyOperationBehavior());
       }
      }
    
    0 讨论(0)
  • 2020-12-10 05:41

    I am building something similar (I think - don't have the time to look through all your code), but have gone about it in a different way.

    To achieve this I am using the following:

    • An IMessageInspector to read the incoming HTTP request message headers (in this case extracting a session Id from a cookie and retrieving a session object from a cache).
    • A combination of an IPrincipal and an IAuthorizationPolicy to implement my own custom authorization code (WCF will automatically invoke my code for requests to web service methods which have the attribute '[PrincipalPermission(SecurityAction.Demand, Role="somerole")]' set).
    • An IErrorHandler which catches any uncaught exceptions from the web service methods (including a permission denied exception thrown if authorization fails -- i.e. the IsRole method you implement in the IPrincipal returns false). If you catch the security denied exception you can then use WebOperationContext.Current to set the custom HTTP error codes for the response message.
    • A custom behavior (an IContractBehavior - but you can also use an EndPoint or Service behavior or whatever you want) which creates all the above at runtime and attaches them to the appropriate endpoints.
    0 讨论(0)
  • 2020-12-10 05:58

    I know this is very old, but for me Alexey's answer worked. However, only when the ApplyDispatchBehaviour method calls the base method. Like this:

    public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
    
            foreach (var operation in endpoint.Contract.Operations)
            {
                if (operation.Behaviors.Contains(typeof(AccessControlOperationBehaviour)))
                    continue;
        
                operation.Behaviors.Add(new AccessControlOperationBehaviour());
            }
        }
    
    0 讨论(0)
提交回复
热议问题