1

カスタム属性を使用した承認が必要なWCFRESTサービスを開発しようとしています。

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

aspnetcompatibilityModeWCFサービスで有効にできない場合は、次のように実行できます。

HttpResponseMessagePropertyメッセージを傍受し、wcfメッセージにステータスコードを設定する必要があります。私はCustomErrorHandlerそれを行うためにを使用し、それはうまく機能します。


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 に答える