This project is read-only.

Logging, or Authentication

If you need to hook into the execution pipeline before your JsonRpcMethod is invoked, then you can do this by making a call to Config.SetPreProcessHandler as early as possible. If your using then you could do this in Global.Application_Start. Ex.
        protected void Application_Start(object sender, EventArgs e) {
            Config.SetErrorHandler(OnJsonRpcException); // Set the Error Handler
            Config.SetPreProcessHandler(new PreProcessHandler(PreProcess)); // Set the pre Processing Handler

This is an example Preprocess handler. It has access to all of the Json-Rpc request information, and also has access to the JsonRpcContext.Current().Value. The context is passed in but also runs under the same context that the JsonRpcMethod does, so calling JsonRpcContext is still valid, though not needed as the context is passed as a parameter.
        /// <summary>
        /// This method is invoked prior to ANY, and ALL JsonRpcMethods that have been registered.
        /// </summary>
        /// <param name="rpc">The JSON-RPC request as it has been parsed</param>
        /// <param name="context">Because this is ASP.NET the context will be set as an instance of HttpRequest</param>
        /// <returns></returns>
        private JsonRpcException PreProcess(JsonRequest rpc, object context)
            // Useful for logging or authentication using the context.

            if(!string.Equals(rpc.Method, "RequiresCredentials",StringComparison.CurrentCultureIgnoreCase))
                return null;
            // If this is using the ASP.Net handler then the context will contain the httpRequest
            // you could use that for cookies or IP authentication.
            var httpRequest = context as HttpRequest;

            // Here we will just check that the first parameter is a magic Key
            // DO NOT do this type of thing in production code. You would be better just checking the parameter inside the JsonRpcMethod. This is here just to show access to the JsonRequest
            var j = rpc.Params as Newtonsoft.Json.Linq.JArray;
            if (j == null 
                || j[0] == null
                || j[0].Type != Newtonsoft.Json.Linq.JTokenType.String
                || !string.Equals(j[0].ToString(), "GoodPassword", StringComparison.CurrentCultureIgnoreCase)
                ) return new JsonRpcException(-2, "This exception was thrown using: JsonRpcTest.Global.PreProcess, Not Authenticated", null);

            return null;

Last edited Jan 24, 2013 at 6:26 PM by AustinHarris, version 2


No comments yet.