ParseRequest Method

Parses and processes the message.

Syntax

public void ParseRequest();
Public Sub ParseRequest()

Remarks

This method processes the message in the request. If the message is encrypted, it will be decrypted. If the message is signed, the signature will be verified. This method should be called after calling ReadRequest and specifying any necessary certificates for the operation to complete successfully.

Receiving Files and Sending a Receipt

When receiving files first check the AgreementRef, AS4From, and AS4To properties to determine who is sending the files and with what previously agreed upon configuration. Once this is known, if the request is signed and encrypted set Certificate to the decryption certificate and SignerCert to the public certificate used for signature verification. IncomingDirectory may optionally be set to automatically store the incoming files.

//Process incoming files and send a signed receipt

server.ReadRequest();

//Inspect values from the request in order to load appropriate certificates etc.
//Console.WriteLine(server.AgreementRef);
//Console.WriteLine(server.AS4From.Id);
//Console.WriteLine(server.AS4To.Id);)

server.IncomingDirectory = "..\\MyFiles";
 
//Our private certificate. Used to decrypt the incoming file
server.Certificate = new Certificate(CertStoreTypes.cstPFXFile, Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyB.pfx"), "password", "*");

//Partner's public certificate. Used to verify the signature on the incoming message and files.
server.SignerCert = new Certificate(Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyA.cer"));

server.ParseRequest();

server.ReceiptReplyMode = As4serverReceiptReplyModes.rrmSync;

//Our private certificate. Used to sign the receipt.
server.SigningCert = new Certificate(CertStoreTypes.cstPFXFile, Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyB.pfx"), "password", "*");

server.SendResponse(); //Sends the receipt
Receiving Files and Sending an Asynchronous Receipt

Receipts may be sent in the response (synchronous) or at a later time (asynchronous). If the agreement specifies that the receipt be sent asynchronously the following steps may be taken to send the receipt.

After calling ReadRequest the ReceiptReplyMode may be set to indicate the receipt will be returned asynchronously. After calling ParseRequest call SendAckResponse to send back a HTTP 200 OK to the client. The receipt may then be returned later.

To send an asynchronous receipt AS4Client may be used. This can be sent to the partner's web site, or bundled with a later response (depending on the agreement made between the parties). In the example below AS4Client is used to send the receipt to the other party's web site.


//Process incoming files and send an asynchronous receipt

server.ReadRequest();

//Inspect values from the request in order to load appropriate certificates etc.
//Console.WriteLine(server.AgreementRef);
//Console.WriteLine(server.AS4From.Id);
//Console.WriteLine(server.AS4To.Id);)

server.IncomingDirectory = "..\\MyFiles";
 
//Our private certificate. Used to decrypt the incoming file
server.Certificate = new Certificate(CertStoreTypes.cstPFXFile, Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyB.pfx"), "password", "*");

//Partner's public certificate. Used to verify the signature on the incoming message and files.
server.SignerCert = new Certificate(Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyA.cer"));

server.ParseRequest();

server.ReceiptReplyMode = As4serverReceiptReplyModes.rrmAsync;

//Our private certificate. Used to sign the receipt.
server.SigningCert = new Certificate(CertStoreTypes.cstPFXFile, Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyB.pfx"), "password", "*");

server.SendAckResponse(); //Sends an ack, but not the receipt
At this point Receipt is populated with the receipt to be sent. Store the Receipt's Content and RefToMessageId values for use when sending the receipt later. Sending a receipt can be done with AS4Client.


//Send an asynchronous receipt
client.URL = ""http://www.company.com:9090/msh"";
client.Receipt = new EBReceipt(server.Receipt.RefToMessageId, server.Receipt.Content);
client.ReceiptReplyMode = As4clientReceiptReplyModes.rrmAsync;
client.SendReceipt();

Sending Files

To process a request to send files first check the MPC property. This holds the Message Partition Channel (MPC) from which the client would like to receive files. Next, set AgreementRef, AS4From, AS4To. Check IncomingReceipt to determine if the request has a bundled receipt. If it does VerifyReceipt can be called to verify the receipt.

Note: If the client requests files from the default MPC then MPC may be empty. See MessageType for details.

If the client makes use of UsernameToken authentication the TokenAuthentication event will fire when processing the request.

To send files back to the client simply set EDIData to the files you wish to send. When SendResponse is called the files will be sent back to the client.


//Process a request to send files (pull)

//Holds information from the original send so that receipts can be verified later
server.AsyncReceiptInfoDir = Path.Combine(Request.PhysicalApplicationPath, "..\\temp\\ReceiptInfoDir")

server.Profile = As4serverProfiles.ebpfENTSOG;
server.ReadRequest();

//The receipt may be signed depending upon the AgreementRef
server.SignerCert = new Certificate(Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyA.cer"));

//If the request has a bundled receipt verify it first
if (!string.IsNullOrEmpty(server.IncomingReceipt.Content))
{
  server.VerifyReceipt();
}

//If the request is a pull request (MPC is set)
if (server.AgreementRef == "" && server.MPC != "")
{
  server.AgreementRef = "http://agreements.company.com/pull_files";
  server.AS4From.Id = "org:holodeckb2b:example:company:B";
  server.AS4From.Role = "Sender";

  server.AS4To.Id = "org:holodeckb2b:example:company:A";
  server.AS4To.Role = "Receiver";

  server.ReceiptReplyMode = As4serverReceiptReplyModes.rrmAsync;

  //Our private certificate. Used to sign the message and files.
  server.SigningCert = new Certificate(CertStoreTypes.cstPFXFile, Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyB.pfx"), "password", "*");

  //Partner's public certificate. Used to encrypt files. 
  server.RecipientCerts.Add(new Certificate(Path.Combine(Request.PhysicalApplicationPath, "..\\files\\CompanyA.cer")));

  EBData data = new EBData();
  data.EDIType = "text/xml";
  data.Data = "<test>Hello AS4 World!</test>";
  server.EDIData.Add(data);

  server.SendResponse();
}

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks EDI 2020 .NET Edition - Version 20.0 [Build 8203]