示例#1
0
        public virtual void OnMessageReceived(LocalHttpListenerEventArgs e)
        {
            EventHandler <LocalHttpListenerEventArgs> handler = Message;

            if (handler != null)
            {
                handler(this, e);
            }
        }
示例#2
0
        /// <summary>
        /// Handle the returned date and extract the code
        /// </summary>
        /// <param name="context"></param>
        /// <param name="response"></param>
        /// <returns>true/false</returns>
        private bool HandleCallbackRequest(HttpListenerContext context, HttpListenerResponse response)
        {
            response.ContentType = "text/html";

            //Write it to the response stream
            var query = context.Request.Url.Query;
            var code  = "";
            var state = "";

            // Deal with access denied/cancel
            if (query.Contains("error"))
            {
                var buffer = Encoding.UTF8.GetBytes(config.AccessDeniedHTML);
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                // Raise the event so the oAuth2 class can process the receipt of the
                LocalHttpListenerEventArgs args = new LocalHttpListenerEventArgs()
                {
                    MessageText = XeroConstants.XERO_AUTH_ACCESS_DENIED
                };
                OnMessageReceived(args);
                config.ReturnedAccessCode = XeroConstants.XERO_AUTH_ACCESS_DENIED;// Asigning this will stop the wait timeout loop
            }
            else
            {
                if (query.Contains("?"))
                {
                    // Break down the query string by the ?
                    query = query.Substring(query.IndexOf('?') + 1);
                }

                foreach (var vp in Regex.Split(query, "&"))
                {
                    var singlePair = Regex.Split(vp, "=");

                    if (singlePair.Length == 2)
                    {
                        if (singlePair[0] == "code")
                        {
                            code = singlePair[1];
                            config.ReturnedAccessCode = code; // Asigning this will stop the wait timeout loop
                        }

                        if (singlePair[0] == "state")
                        {
                            state = singlePair[1];
                            config.ReturnedState = state;
                        }
                    }
                }

                var buffer = Encoding.UTF8.GetBytes(config.AccessGrantedHTML);
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                // Raise the event so the oAuth2 class can process the receipt of the
                LocalHttpListenerEventArgs args = new LocalHttpListenerEventArgs()
                {
                    MessageText = XeroConstants.XERO_AUTH_ACCESS_GRANTED
                };
                OnMessageReceived(args);

                return(true);
            }
            return(false);
        }