none
WCF REST JSON - how to respond json from CheckAccessCore function when validation not pass RRS feed

  • Question

  • hi,experts,my wcf rest json platform is for mobile devices (android +ios) data interfaces. .

        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- This behavior enables API Key Verification -->
              <serviceAuthorization serviceAuthorizationManagerType="xxxMobile.xxxMobileAuthorizationManager, xxxMobile" />                 
            </behavior>
          </serviceBehaviors>
        </behaviors>


    I select to use a "customized" serviceAuthorizationManager and the function 

    protected override bool CheckAccessCore(OperationContext operationContext)


    to do user authentication/validation.

    if it returns false in the CheckAccessCore function, the request should be stop.

    I would like to customize the response if the validation in CheckAccessCore fails( return false).

    in the response, I need to provide authentication failure information to mobile devices (show reasons of failure on mobile screen).

    I need the information in json format.

    I create a class as below

    [DataContract]
        public class xxxMobileError
        {
            [DataMember]
            public string ErrorCode { get; set; }
            [DataMember]
            public string ErrorMessage { get; set; }
    
        }

    I try to write a function to construct the response message but I do not whether I have done in correct way.

    private static void CreateErrorReplyJSON(OperationContext operationContext)
            {
                xxxMobileError error = new xxxMobileError();
                error.ErrorCode = "B";
                error.ErrorMessage = "At least one required field is blank.";
    
    
                Message fault = Message.CreateMessage(MessageVersion.None
                    , ""
                    , error
                    , new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(xxxMobileError)));
                var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
                fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);
    
                //var response = WebOperationContext.Current.OutgoingResponse;
                //response.ContentType = "application/json";
                //response.StatusCode = HttpStatusCode.InternalServerError;
    
                
                operationContext.RequestContext.Reply(fault);
                operationContext.RequestContext = null;
            }


     I get this message when I access something likes http://localhost:52450/Mobile/Apple/999

    The XML page cannot be displayed

    Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


    Invalid at the top level of the document. Error processing resource 'http://localhost:52450/Mobile/Apple/999'. Line 1, Positi...

    {"ErrorCode":"B","ErrorMessage":"At least one required field is blank."}
    ^


    and I found that by this approach, it actually respond as a XML file. I tried to call from chrome, it returns a xml file. is it possible to respond as a json string only?

    • Edited by delaynomore Thursday, February 20, 2014 9:43 AM
    Thursday, February 20, 2014 9:36 AM

All replies