public static void SignXadesBes(XmlSignatureAppearance sap, IExternalSignature externalSignature, X509Certificate[] chain) {
VerifyArguments(sap, externalSignature);
String contentReferenceId = SecurityConstants.Reference_ + GetRandomId();
String signedPropertiesId = SecurityConstants.SignedProperties_ + GetRandomId();
String signatureId = SecurityConstants.Signature_ + GetRandomId();
XmlDocument doc = sap.GetXmlLocator().GetDocument();
KeyInfoClause keyInfo = GenerateKeyInfo(chain, sap);
List<XmlElement> references = new List<XmlElement>(2);
XmlElement signature = GenerateSignatureElement(sap.GetXmlLocator(), signatureId, true);
XmlElement signedProperty;
XmlElement dsObject = GenerateXadesBesObject(sap, signatureId, contentReferenceId, signedPropertiesId, out signedProperty);
references.Add(GenerateCustomReference(doc, signedProperty, "#" + signedPropertiesId, SecurityConstants.SignedProperties_Type, null));
references.Add(GenerateContentReference(doc, sap, contentReferenceId));
Sign(signature, sap.GetXmlLocator(), externalSignature, references, dsObject, keyInfo);
sap.Close();
}