public async Task <IHttpActionResult> ProvisioningRequest()
        {
            log.Debug("ProvisioningRequest(.) started.");

            bool              error             = false;
            HttpStatusCode    errorCode         = System.Net.HttpStatusCode.InternalServerError;
            ChallengeResponse challengeResponse = null;

            try
            {
                challengeResponse = await ClientDatabase.NewRequest(this.Request);
            }
            catch (WebException we)
            {
                error = true;
                HttpWebResponse WebResponse = we.Response as HttpWebResponse;
                log.Debug("Web Exception in Provisioning Request: " + we.Message);
                errorCode = (HttpStatusCode)WebResponse.StatusCode;
            }
            catch (HttpResponseException re)
            {
                error = true;
                log.Debug("HttpResponseException in Provisioning Request: " + re.Message);
                errorCode = (HttpStatusCode)re.Response.StatusCode;
            }
            catch (Exception e)
            {
                error = true;
                log.Debug("Error occurred processing provision request. " + e.Message);
                errorCode = System.Net.HttpStatusCode.InternalServerError;
            }

            // Return response to client
            if (error)
            {
                log.DebugFormat("ProvisioningRequest(.) returning HTTP status code {0}.", errorCode);
                try
                {
                    if (errorCode != (HttpStatusCode)System.Net.HttpStatusCode.Conflict)
                    {
                        ClientDatabase.RemoveTransaction(this.Request, Constants.ProvisionStr);
                    }
                }
                catch (Exception e)  // This catches HttpResponseException also, which is what we want here
                {
                    log.Debug("Exception in provision request while error cleanup RemoveTransaction(): " + e.Message);
                }
                return(StatusCode(errorCode));
            }
            else
            {
                log.Debug("ProvisioningRequest(.) returning HTTP status success.");
                return(Json(challengeResponse));
            }
        }
        //Build a challenge response message
        public void buildChallengeResponse(out ChallengeResponse cMResp)
        {
            string respond = Constants.Respond;
            var    cMsg    = new ChallengeResponse(respond);

            //populate the message object with header and body components
            var cMsgBody = new ResponseChallengeMsgBody();

            cMsg.respHeader.sessionNonce = Constants.sn1;
            cMsg.cRespBody = cMsgBody;

            //assign a return object reference
            cMResp = cMsg;
            return;
        }
        /// <summary>
        /// Create a new Provision/Remote Attestation request
        /// Processes provision request and creates a Challenge response to send to a client
        /// </summary>
        /// <param name="data">Thread Data, input parameter (HttpRequestMessage) from the client</param>
        public void CreateNewRequest(object data)
        {
            challengeResponse = null;

            log.Debug("CreateNewRequest(.) started.");

            try
            {
                if (data == null)
                {
                    HttpResponseException e = new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
                    options.LogThrownException(e);
                    throw e;
                }

                HttpRequestMessage request = (HttpRequestMessage)data;

                var    result                     = request.Content.ReadAsStringAsync();
                string jsonMsgRequest             = result.Result;
                ProvisionRequestMessage pReceived = JsonConvert.DeserializeObject <ProvisionRequestMessage>(jsonMsgRequest);


                // Get client ID so we can track it
                string clientID = ClientDatabase.GetClientID(request, Constants.ProvisionStr);
                mClient = new ClientTransaction(clientID);
                mClient.sigmaSequenceCheck.currentNonce = pReceived.reqHeader.nonce;


                ProvisionRequest  provRequest        = new ProvisionRequest();
                ChallengeResponse tChallengeResponse = provRequest.ProcessProvisionRequest(request, mClient);

                // Add new client to request database only if successful
                if (!ClientDatabase.AddClient(mClient))
                {
                    HttpResponseException e = new HttpResponseException(System.Net.HttpStatusCode.Conflict);
                    options.LogThrownException(e);
                    throw e;
                }

                log.Info("\n ***** State: Starting Provision request for client: " + mClient.ID + "\n");

                // Update client state
                if (!mClient.sigmaSequenceCheck.UpdateState(Constants.SequenceState.Challenge))
                {
                    HttpResponseException e = new HttpResponseException(System.Net.HttpStatusCode.PreconditionFailed);
                    options.LogThrownException(e);
                    throw e;
                }

                // Set Client State
                challengeResponse = tChallengeResponse;
            }
            catch (HttpResponseException re)
            {
                options.LogCaughtException(re);
                httpRE = re;
            }
            catch (Exception ex)
            {
                options.LogCaughtException(ex);
                threadException = ex;
            }
            finally
            {
                log.Debug("CreateNewRequest(.) returning.");
            }
        }