0
votes

i have a SimplaKeyAuthorizeFilter,in the filter,will throw 401

public SimpleKeyAuthorize(IHttpRequest req, IHttpResponse res, object obj, bool isDebugMode){
    throw new UnauthorizedAccessException("lack sign info!");
}

in the apphost:

this.ExceptionHandler = (httpReq, httpRes, operationName, ex) =>{
    DtoUtils.CreateErrorResponse(httpReq, ex, new ResponseStatus() { ErrorCode = ex.ToErrorCode(), Message = ex.Message });
    httpRes.Write(ex.ToErrorCode());
    httpRes.EndRequest(skipHeaders:false);
};

........
this.RequestFilters.Add((req,res,obj)=>{
    new SimpleKeyAuthorize(req,res,obj,false);
});

but when i call the service,the httpstatuscode is 200,is not 401,why?

HTTP/1.1 200 OK (why not 401?)

Cache-Control: private

Content-Type: text/html; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 14 Mar 2014 05:56:30 GMT

Content-Length: 27

UnauthorizedAccessException

1

1 Answers

1
votes

This is happening because ServiceStack only maps exceptions to status code within the scope of the service.

Request bindings and filters are considered outside this scope, which is why they trigger a separate exception handler method. See the section Customized Error Handling in the Error Handling documentation.

If you throw an exception here you will need to set the response status code yourself.

httpRes.StatusCode = HttpStatusCode.Unauthorized;

Hope this helps.