using System.Web; using System.Web.Services; using SAP.Middleware.Connector; ////// Descripción breve de WebService /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. // [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //Eliminar la marca de comentario de la línea siguiente si utiliza los componentes diseñados //InitializeComponent(); } [WebMethod] public string TraeCompania() { RfcConfigParameters Conexion = new RfcConfigParameters(); //Parametros de conexión Conexion.Add(RfcConfigParameters.Name, "TEST"); Conexion.Add(RfcConfigParameters.AppServerHost, "10.0.0.xx"); Conexion.Add(RfcConfigParameters.SystemNumber, "00"); Conexion.Add(RfcConfigParameters.SystemID, ""); Conexion.Add(RfcConfigParameters.SAPRouter, "/H/200.6.96.xx/H/"); Conexion.Add(RfcConfigParameters.User, "xxx"); Conexion.Add(RfcConfigParameters.Password, "xxx"); Conexion.Add(RfcConfigParameters.Client, "300"); Conexion.Add(RfcConfigParameters.Language, "en"); Conexion.Add(RfcConfigParameters.PoolSize, "5"); Conexion.Add(RfcConfigParameters.PeakConnectionsLimit, "10"); Conexion.Add(RfcConfigParameters.ConnectionIdleTimeout, "600"); RfcDestination ConxSap = RfcDestinationManager.GetDestination(Conexion); //creacion del objeto que nos permitira conectarnos a SAP try { RfcRepository repositorio = ConxSap.Repository;//nos conectamos a SAP //llamada a la funcion BAPI_COMPANY_GETDETAIL IRfcFunction sd = repositorio.CreateFunction("BAPI_COMPANY_GETDETAIL"); //Asignando valores sd.SetValue("companyid", "BP0001");//parametro de entreda //Enviando valores sd.Invoke(ConxSap); return (String)sd.GetStructure("COMPANY_DETAIL").GetValue("NAME1");//retornamos el campo NAME1 dentro de la estructura COMPANY_DETAIL que devuelve la funcion } catch(Exception ex) { return ex.Message; } } }
Donde Leonel
Programación y algo más
jueves, 9 de enero de 2014
Conectar .NET con SAP (C#)
Para poder conectarnos desde cualquier aplicacion .net a SAP es recomendable usar un webservice, ya que, el conector de SAP para .Net solo funciona en ASP .NET. Para ello crearemos un WebMethod el cual le llamaremos TraeCompania, la cual, se encargara de traer los datos de la compañia filtrando por el codigo de esta.
miércoles, 25 de julio de 2012
Cifrar elementos XML con certificados X.509
using System; using System.Xml; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Security.Cryptography.X509Certificates; class Program { static void Main(string[] args) { try { // Creando un objeto XmlDocument. XmlDocument xmlDoc = new XmlDocument(); // cargando un archivo XML en el objeto XmlDocument. xmlDoc.PreserveWhitespace = true; xmlDoc.Load("test.xml"); // Abrir el Almacen X.509 "CurrentUser" en modo solo lectura. X509Store store = new X509Store(StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); // Coloca todos los certificados en un objeto X509Certificate2Collection. X509Certificate2Collection certCollection = store.Certificates; X509Certificate2 cert = null; // Recorrer cada certificado y encontrar el certificado // con el nombre apropiado. foreach (X509Certificate2 c in certCollection) { if (c.Subject == "CN=XML_ENC_TEST_CERT") { cert = c; break; } } if (cert == null) { throw new CryptographicException("El certificado X.509 no se pudo eliminar."); } // Cierra el almacen. store.Close(); // Encripta el elemento "creditcard". Encrypt(xmlDoc, "creditcard", cert); // Graba el documento XML. xmlDoc.Save("test.xml"); // Muestra el Xml encriptado en la consola. Console.WriteLine("Encrypted XML:"); Console.WriteLine(); Console.WriteLine(xmlDoc.OuterXml); } catch (Exception e) { Console.WriteLine(e.Message); } } public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, X509Certificate2 Cert) { // Chequea el argumento if (Doc == null) throw new ArgumentNullException("Doc"); if (ElementToEncrypt == null) throw new ArgumentNullException("ElementToEncrypt"); if (Cert == null) throw new ArgumentNullException("Cert"); /////////////////////////////////////////////////// // Busca el elemento especificado en el objeto // XmlDocument y crea un nuevo objeto XmlElemnt. /////////////////////////////////////////////////// XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement; // Genera una XmlException si el elemento no es encontrado. if (elementToEncrypt == null) { throw new XmlException("El elemento no ha sido encontrado"); } //////////////////////////////////////////////////// // Crea una nueva instancia de la clase EncryptedXml // y usa el XmlElement encriptado con el // Certificado X.509. //////////////////////////////////////////////////// EncryptedXml eXml = new EncryptedXml(); // Encripta el elemento. EncryptedData edElement = eXml.Encrypt(elementToEncrypt, Cert); //////////////////////////////////////////////////// // Remplaza el elemento del objeto XmlDocument original // con el elemento EncryptedData. //////////////////////////////////////////////////// EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); } }
Suscribirse a:
Entradas (Atom)