1

我正在尝试开发一个需要使用自定义属性授权的 WCF 休息服务。

如果授权密钥在实现 IOperationBehavior 和 IParameterInspector 的自定义属性中无效,我想将响应作为 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)
{
}
4

4 回答 4

2

抛出 WebFormatException

throw new WebFaultException(HttpStatusCode.Unauthorized);
于 2011-06-17T14:05:46.317 回答
0

如果您使用的是 WCF Rest Starter 工具包,您还可以执行以下操作:

throw new Microsoft.ServiceModel.Web.WebProtocolException(System.Net.HttpStatusCode.Unauthorized, "message", ex);

如果需要,该方法还有更多重载。

于 2011-06-17T15:56:26.470 回答
0
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");
于 2016-06-23T15:33:09.897 回答
0

如果aspnetcompatibilityMode无法在您的 WCF 服务中启用,那么您可以执行以下操作。

您必须拦截该消息并HttpResponseMessageProperty在 wcf 消息中设置状态代码。我使用 aCustomErrorHandler来做到这一点,它工作正常。


public class CustomErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            return true;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
              fault.Properties[HttpResponseMessageProperty.Name] = new HttpResponseMessageProperty() 
              { 
                    StatusCode = statusCode 
              };
        }
    }

下面的代码用于将 CustomErrorHandler 注入ServiceBehavior.

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>
于 2019-10-17T10:35:30.047 回答