using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml; using System.Xml.XPath; using System.Text; using System.Net; using System.Net.Security; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService, IWarrantPortBinding { private ClientCertStatus clientCertStatus; private StringBuilder sb; private enum ClientCertStatus { NOERRORS, TIMEERROR, NOTTRUSTED, CNNOTMATCH, NOTPRESENT }; public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } private ClientCertStatus ValidateClientCert() { HttpClientCertificate cert = Context.Request.ClientCertificate; #region VerboseDebugging if (cert != null) sb = new StringBuilder(); DateTime dtNow = DateTime.Now; sb.Append("Client Certificate Received:\t"); sb.Append(dtNow.ToString("yyyy-MM-dd hh:mm:ss tt")); sb.Append(Environment.NewLine); sb.Append("IsPresent:\t"); sb.Append(cert.IsPresent); sb.Append(Environment.NewLine); sb.Append("Encoding:\t"); sb.Append(cert.CertEncoding); sb.Append(Environment.NewLine); sb.Append("Issuer:\t"); sb.Append(cert.Issuer); sb.Append(Environment.NewLine); //The client certificate is not valid if the Certificate //Authority (CA) is not in the list of recognized CAs on the server. sb.Append("IsValid:\t"); sb.Append(cert.IsValid); sb.Append(Environment.NewLine); sb.Append("SerialNumber:\t"); sb.Append(cert.SerialNumber); sb.Append(Environment.NewLine); sb.Append("ServerIssuer:\t"); sb.Append(cert.ServerIssuer); sb.Append(Environment.NewLine); sb.Append("ServerSubject:\t"); sb.Append(cert.ServerSubject); sb.Append(Environment.NewLine); sb.Append("Subject:\t"); sb.Append(cert.Subject); sb.Append(Environment.NewLine); sb.Append("ValidFrom:\t"); sb.Append(cert.ValidFrom.ToString("yyyy-MM-dd hh:mm:ss tt")); sb.Append(Environment.NewLine); sb.Append("ValidUntil:\t"); sb.Append(cert.ValidUntil.ToString("yyyy-MM-dd hh:mm:ss tt")); sb.Append(Environment.NewLine); #endregion #region Check to see that cert is present if (!cert.IsPresent) { return ClientCertStatus.NOTPRESENT; } #endregion #region Check to see that cert is valid, trusted ca if (!cert.IsValid) { return ClientCertStatus.NOTTRUSTED; } #endregion #region Check to see that cert is from a machine that we actually trust if (cert.Subject.Contains("CN=labdc.doa.wistate.us") || cert.Subject.Contains("CN=wijis.wisconsin.gov") || cert.Subject.Contains("CN=wijisgwtest.wisconsin.gov")) { } else { ApplicationEventLog.WriteError(cert.Subject + Environment.NewLine + "Attempted to access the service and is not allowed to do so." + Environment.NewLine); return ClientCertStatus.CNNOTMATCH; //TODO: Eventually you probably want to throw an exception here to prevent //anyone with a certificate that you are not expecting to not get access to the service //throw new Exception("Invalid Certificate"); } #endregion #region Check to see that cert is within the valid time window if (DateTime.Now > cert.ValidUntil || DateTime.Now < cert.ValidFrom) { return ClientCertStatus.TIMEERROR; } #endregion return ClientCertStatus.NOERRORS; } [WebMethod] public XmlDocument submit(object thisObject) { XmlNode[] thisNode = thisObject as XmlNode[]; XmlNode headNode = thisNode[1]; String innerXML = headNode.InnerXml; // Create an XmlNamespaceManager to resolve the default namespace. XmlNamespaceManager nsmgr = new XmlNamespaceManager(headNode.OwnerDocument.NameTable); nsmgr.AddNamespace("doc", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/document/"); nsmgr.AddNamespace("j", "http://www.it.ojp.gov/jxdm/3.0.3"); nsmgr.AddNamespace("warrant", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); this.clientCertStatus = ValidateClientCert(); if (this.clientCertStatus.Equals(ClientCertStatus.NOERRORS)) { #region get element values. StringBuilder stringBuilder = new StringBuilder(); // WarrantIssueDocument id String docId = headNode.SelectSingleNode("./@j:id", nsmgr).InnerText; // Warrant Node. XmlNode warrantNode = headNode.SelectSingleNode("./warrant:Warrant", nsmgr); stringBuilder.AppendLine("Document Id :" + docId); String warrantId = warrantNode.SelectSingleNode("./@j:id", nsmgr).InnerText; stringBuilder.AppendLine("Warrant Id :" + warrantId); // CourtOrderDesignatedLocation string courtOrderDesignatedLocationStr = HelperMethods.getCourtOrderDesignatedLocation(warrantNode, nsmgr); stringBuilder.AppendLine("\nCourtOrderDesignatedLocation: \n" + courtOrderDesignatedLocationStr); // CourtOrderIssuingJudicialOfficial string courtOrderIssuingJudicialOfficial = HelperMethods.getCourtOrderIssuingJudicialOfficial(warrantNode, nsmgr); stringBuilder.AppendLine("CourtOrderIssuingJudicialOfficial: \n" + courtOrderIssuingJudicialOfficial); // CourtOrderIssuingDate string courtOrderIssuingDate = warrantNode.SelectSingleNode("./j:CourtOrderIssuingDate", nsmgr).InnerText; stringBuilder.AppendLine("CourtOrderIssuingDate: " + courtOrderIssuingDate); // WarrantAppearanceBail XmlNode bailSetAmmountNode = warrantNode.SelectSingleNode(".//j:BailSetAmount", nsmgr); if (bailSetAmmountNode != null) { stringBuilder.AppendLine("WarrantApearanceBail: " + bailSetAmmountNode.InnerText); } // WarrantExtraditionLimitationText XmlNode warrantExtraditionlimitationTextNode = warrantNode.SelectSingleNode("./j:WarrantExtraditionLimitationText", nsmgr); if (warrantExtraditionlimitationTextNode != null) { stringBuilder.AppendLine("WarrantExtraditionLimitationText: " + warrantExtraditionlimitationTextNode.InnerText); } // WarrantExtraditionLimitationCode XmlNode warrantExtraditionlimitationCodeNode = warrantNode.SelectSingleNode("./j:WarrantExtraditionLimitationCode", nsmgr); if (warrantExtraditionlimitationTextNode != null) { stringBuilder.AppendLine("WarrantExtraditionLimitationCode: " + warrantExtraditionlimitationCodeNode.InnerText); } // CaseDocketID string caseDocketID = warrantNode.SelectSingleNode(".//j:CaseDocketID/j:ID", nsmgr).InnerText; stringBuilder.AppendLine("Case:\n\tCase Docket ID: " + caseDocketID); // WarrantActionStatus string warrantActionStatus = HelperMethods.getWarrantActionStatus(warrantNode, nsmgr); stringBuilder.AppendLine("WarrantActionStatus: \n " + warrantActionStatus); // WarrantCode string warrantCode = warrantNode.SelectSingleNode("./warrant:WarrantCode", nsmgr).InnerText; stringBuilder.AppendLine("Warrant Code: " + warrantCode); XmlNode warrantCodeDescNode = warrantNode.SelectSingleNode("./warrant:WarrantCodeDescription", nsmgr); if (warrantCodeDescNode != null) { stringBuilder.AppendLine("Warrant Code Description: " + warrantCodeDescNode.InnerText); } // WarrantSubject stringBuilder.AppendLine(HelperMethods.getWarrantSubject(warrantNode, nsmgr)); stringBuilder.AppendLine(HelperMethods.getCharges(warrantNode, nsmgr)); // Optional warrant:WarrantGeographicRestrictionCode XmlNode warrantGeographicRestrictionCodeNode = warrantNode.SelectSingleNode("./warrant:WarrantGeographicRestrictionCode", nsmgr); if (warrantGeographicRestrictionCodeNode != null) { stringBuilder.AppendLine("Warrant Geographic RestrictionCode: " + warrantGeographicRestrictionCodeNode.InnerText); } //Optional warrant:WarrantGeographicRestrictionDescription XmlNode warrantGeographicRestrictionDescNode = warrantNode.SelectSingleNode("./warrant:WarrantGeographicRestrictionDescription", nsmgr); if (warrantGeographicRestrictionDescNode != null) { stringBuilder.AppendLine("Warrant Geographic Restriction Description: " + warrantGeographicRestrictionDescNode.InnerText); } stringBuilder.AppendLine(HelperMethods.getWarrantDocuments(warrantNode, nsmgr)); //return stringBuilder.ToString(); #endregion XmlDocument responseDoc = this.createResponseDocument(headNode, nsmgr); return responseDoc; } else { throw new Exception("The cert is not valid: " + this.clientCertStatus); } } private XmlDocument createResponseDocument(XmlNode warrantIssueDocumentNode, XmlNamespaceManager nsmgr) { XmlDocument responseDoc = new XmlDocument(); XmlElement rootElement = responseDoc.CreateElement("doc:WarrantResponseDocument", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/document/"); rootElement.SetAttribute("xmlns:doc", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/document/"); rootElement.SetAttribute("xmlns:j", "http://www.it.ojp.gov/jxdm/3.0.3"); rootElement.SetAttribute("xmlns:warrant", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); rootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); rootElement.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/document/WarrantResponse/WarrantResponse.xsd"); string docId = warrantIssueDocumentNode.SelectSingleNode("./@j:id", nsmgr).InnerText; rootElement.SetAttribute("id", "http://www.it.ojp.gov/jxdm/3.0.3", docId); responseDoc.AppendChild(rootElement); // WarrantDescriptiveMetadata XmlElement warrantDesciptiveMetadata = responseDoc.CreateElement("warrant:WarrantDescriptiveMetadata", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); XmlElement documentReceiveDate = responseDoc.CreateElement("j:DocumentReceivedDate", "http://www.it.ojp.gov/jxdm/3.0.3"); documentReceiveDate.InnerText = DateTime.Now.ToString("yyyy-MM-dd"); warrantDesciptiveMetadata.AppendChild(documentReceiveDate); XmlElement documentReceivedTime = responseDoc.CreateElement("warrant:DocumentReceivedTime", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); documentReceivedTime.InnerText = DateTime.Now.ToString("hh:MM:ss"); warrantDesciptiveMetadata.AppendChild(documentReceivedTime); rootElement.AppendChild(warrantDesciptiveMetadata); // Warrant XmlNode warrantNode = warrantIssueDocumentNode.SelectSingleNode("./warrant:Warrant", nsmgr); string warrantId = warrantNode.SelectSingleNode("./@j:id", nsmgr).InnerText; XmlElement warrant = responseDoc.CreateElement("warrant:Warrant", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); warrant.SetAttribute("id", "http://www.it.ojp.gov/jxdm/3.0.3", warrantId); rootElement.AppendChild(warrant); XmlNode courtOrderDesignatedLocation = warrantNode.SelectSingleNode("./j:CourtOrderDesignatedLocation", nsmgr); warrant.AppendChild(responseDoc.ImportNode(courtOrderDesignatedLocation, true)); XmlNode caseNode = warrantNode.SelectSingleNode("./j:Case", nsmgr); warrant.AppendChild(responseDoc.ImportNode(caseNode, true)); XmlElement warrantSubject = responseDoc.CreateElement("warrant:WarrantSubject", "http://wijis.wisconsin.gov/specs/schemas/court-docs/warrant/v1.2/2008-06-23/extension/"); warrant.AppendChild(warrantSubject); XmlNode subjectName = warrantNode.SelectSingleNode("./warrant:WarrantSubject/j:PersonName", nsmgr); warrantSubject.AppendChild(responseDoc.ImportNode(subjectName, true)); XmlNode subjectBirthDate = warrantNode.SelectSingleNode("./warrant:WarrantSubject/j:PersonBirthDate", nsmgr); warrantSubject.AppendChild(responseDoc.ImportNode(subjectBirthDate, true)); return responseDoc; } [WebMethod] public NotificationResponseType notify(object thisObject) { return null; } }