如何将状态代码作为未经授权请求的响应发送到WCF rest服务

如何将状态代码作为未经授权请求的响应发送到WCF rest服务,wcf,http-status-code-401,Wcf,Http Status Code 401,我正在尝试开发一个WCF rest服务,它需要使用自定义属性进行授权 如果授权密钥在实现IOperationBehavior和IPParameterInspector的自定义属性中无效,我希望将响应作为401状态码发送 有人能告诉我如何从自定义属性发送401状态代码作为响应吗 下面是实现 public class AuthorizationAttribute : Attribute,IOperationBehavior, IParameterInspector { #region IOper

我正在尝试开发一个WCF rest服务,它需要使用自定义属性进行授权

如果授权密钥在实现IOperationBehavior和IPParameterInspector的自定义属性中无效,我希望将响应作为401状态码发送

有人能告诉我如何从自定义属性发送401状态代码作为响应吗

下面是实现

public class AuthorizationAttribute : Attribute,IOperationBehavior,
IParameterInspector
{

 #region IOperationBehavior Members 

 public void ApplyDispatchBehavior(OperationDescription operationDescription,
 System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
 {
  dispatchOperation.ParameterInspectors.Add(this);
 }  
 #endregion

 #region IParameterInspector Members

 public object BeforeCall(string operationName, object[] inputs)
 {          

  string publicKey =
  WebOperationContext.Current.IncomingRequest.Header["Authorization"];

   if (publicKey == "592F2D7E-5E9C-400D-B0AE-1C2603C34137")
   {

   } 
   else
   {
    // Here i would like to send the response back to client 
     with the status code       
   }

 }

 return null;

}

 #endregion

}


[Authorization]
public bool signin(string username, string password)
{
}

抛出WebFormatException

throw new WebFaultException(HttpStatusCode.Unauthorized);

如果您正在使用WCF Rest初学者工具包,还可以执行以下操作:

throw new Microsoft.ServiceModel.Web.WebProtocolException(System.Net.HttpStatusCode.Unauthorized, "message", ex);
如果需要,该方法还有一些重载

public string CreateError(int code ,string description)
        {
            Context.Response.StatusCode = code;
            Context.Response.StatusDescription = description;
            Context.ApplicationInstance.CompleteRequest();
            return null;
        }
然后从您的web服务方法中,仅使用此返回错误示例:

return CreateError(403, "Request denied by server");

如果无法在WCF服务中启用
aspnetcompatibilityMode
,则可以执行以下操作

您必须截获消息并在wcf消息的HttpResponseMessageProperty中设置状态代码。我使用一个
CustomErrorHandler
来做这件事,它工作得很好



下面的代码用于将CustomErrorHandler注入到服务行为中

public class CustomServiceBehaviour : IServiceBehavior
{
    ... other IServiceBehavior methods

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
        {
            channelDispatcher.ErrorHandlers.Add(new CustomErrorHandler());
        }
    }
}
然后在web.config中使用serviceBehavior

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="CustomServiceBehavior" type="MyNamespace.CustomServiceBehavior, MyAssembly" />
        </behaviorExtensions>
    </extensions>
    <behaviors>
        <serviceBehaviors>
            <behavior name="CustomBehavior">
                <CustomServiceBehavior />
            </behavior>
      </serviceBehaviors>
    </behaviors>
    <service behaviorConfiguration="CustomBehavior" name="SomeService">
        <endpoint ..../>
    </service>
</system.serviceModel>


我正在研究如何结束请求,并从自定义属性本身将带有状态代码的空响应发送回客户端。有什么办法可以做到这一点吗?我使用的是Framework3.5,我尝试了WebProtocolException。然后我得到了一个错误,用户没有处理异常。同样在客户端,我得到的状态代码是500内部服务器错误。是否有方法通过使用3.5 framework在自定义属性中使用401状态代码来结束请求并发送响应。您可以尝试将WebOperationContext.Current.OutgoingResponse.StatusCode设置为unauthorized并抛出FaultException我尝试抛出FaultException但返回错误“用户未处理异常”。是否有办法处理此异常并将状态代码发送回客户端。不幸的是,返回的状态代码仍然是500(即使使用WebOperationContext.Current.OutgoingResponse.StatusCode或HttpContext.Current.Response.StatusCode=401),只有里面的消息说401I正在使用framework 3.5,我尝试了WebProtocolException。然后我得到了一个错误,用户没有处理异常。同样在客户端,我得到的状态代码是500内部服务器错误。是否有办法使用3.5框架在自定义属性中使用401状态代码结束请求并发送响应。我也使用3.5框架,没有问题。你不应该把它从操作行为上扔下来。为了检查身份验证,我刚刚从WCF REST初学者工具包中实现了RequestInterceptor。嗨,托马斯,我将无法使用RequestInterceptor,因为我使用的是WebScriptServiceHostFactory。由于我使用的是Framework3.5,是否有其他方法可以达到同样的效果。我使用WebProtocolException和IErrorHandler以POX和Json格式发送rest响应的状态代码。
<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="CustomServiceBehavior" type="MyNamespace.CustomServiceBehavior, MyAssembly" />
        </behaviorExtensions>
    </extensions>
    <behaviors>
        <serviceBehaviors>
            <behavior name="CustomBehavior">
                <CustomServiceBehavior />
            </behavior>
      </serviceBehaviors>
    </behaviors>
    <service behaviorConfiguration="CustomBehavior" name="SomeService">
        <endpoint ..../>
    </service>
</system.serviceModel>