Example #1
0
        private long LoginRSA()
        {
            long lLoginError;

            try
            {
                // creo la struttura dei parametri
                m_UserSession = new Archiva.ArchivaASP_LOGIN();
                m_UserSession.SessionId = 0;
                m_UserSession.Application = txtApplication.Text;
                m_UserSession.User = txtUser.Text;
                m_UserSession.Password = txtPassWord.Text;

                // creo una index list vuota
                ArchivaASP_INDEXLIST IndexList = new ArchivaASP_INDEXLIST();

                // Crypto service
                ArchivaASP_CRY Crypto = new ArchivaASP_CRY();

                // cripto la descrizione XML dei parametri
                byte[] abXmlUserSession = Crypto.EncryptXML(m_UserSession, m_UserSession.GetType(), m_RSA_Server);
                // cripto la chiave del client XML dei parametri
                byte[] abXmlUserKey = Crypto.EncryptXML(m_UserKey, m_UserKey.GetType(), m_RSA_Server);

                byte[] abXmlIndexList = null;

                // effettuo il login
                this.Cursor = Cursors.WaitCursor;
                lLoginError = m_ArchivaASP.Login(ref abXmlUserSession, abXmlUserKey, out abXmlIndexList);
                this.Cursor = Cursors.Default;

                // decripto le info di login
                if (lLoginError == 0)
                {
                    m_UserSession = (ArchivaASP_LOGIN)Crypto.DecryptXML(abXmlUserSession, typeof(ArchivaASP_LOGIN), m_RSA_Client);
                    IndexList = (ArchivaASP_INDEXLIST)Crypto.DecryptXML(abXmlIndexList, typeof(ArchivaASP_INDEXLIST), m_RSA_Client);

                    txtSessionId.Text = m_UserSession.SessionId.ToString();

                    lstIndex.Items.Clear();
                    for (int l = 0; l < IndexList.Indexes.Count; l++)
                        lstIndex.Items.Add((String)IndexList.Indexes[l]);
                }

                return lLoginError;
            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return -1;
            }
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            _service = new biz.archiva.servizi.ArchivaASP {Timeout = 60*60*1000};

            long mRsaKeyLength = 0;

            //=========================================================================
            // OGGETTI PER CRITTOGRAFIA
            //=========================================================================
            // ottengo la chiave pubblica del server
            var sKey = "";
            if (_service.GetKey(ref sKey, ref mRsaKeyLength) != 0)
                return;

            // creo il servizio RSA per il server
            _mRsaServer = new RSACryptoServiceProvider((int)mRsaKeyLength);
            var serverKey = (ArchivaASP_KEY)ArchivaASP_SERIALIZER.DeserializeObject(sKey, typeof(ArchivaASP_KEY));
            var serverParameters = new RSAParameters {Exponent = serverKey.Exp, Modulus = serverKey.Mod};
            _mRsaServer.ImportParameters(serverParameters);

            //=========================================================================
            // LOGIN
            //=========================================================================

            // creo il servizio RSA per il client
            _mRsaClient = new RSACryptoServiceProvider((int)mRsaKeyLength);
            var rsaParam = _mRsaClient.ExportParameters(false);
            _mUserKey = new ArchivaASP_KEY {Mod = rsaParam.Modulus, Exp = rsaParam.Exponent};

            // creo la struttura dei parametri
            _mUserSession = new ArchivaASP_LOGIN { SessionId = 0, Application = "STUDIOZENITHWEB", User = "******", Password = "******" };

            // creo una index list vuota
            var indexList = new ArchivaASP_INDEXLIST();

            // Crypto service
            var crypto = new ArchivaASP_CRY();

            // cripto la descrizione XML dei parametri
            byte[] abXmlUserSession = crypto.EncryptXML(_mUserSession, _mUserSession.GetType(), _mRsaServer);
            // cripto la chiave del client XML dei parametri
            byte[] abXmlUserKey = crypto.EncryptXML(_mUserKey, _mUserKey.GetType(), _mRsaServer);

            byte[] abXmlIndexList;

            // effettuo il login
            long lLoginError = _service.Login(ref abXmlUserSession, abXmlUserKey, out abXmlIndexList);

            // decripto le info di login
            if (lLoginError == 0)
            {
                _mUserSession = (ArchivaASP_LOGIN)crypto.DecryptXML(abXmlUserSession, typeof(ArchivaASP_LOGIN), _mRsaClient);
                indexList = (ArchivaASP_INDEXLIST)crypto.DecryptXML(abXmlIndexList, typeof(ArchivaASP_INDEXLIST), _mRsaClient);
            }

            //=========================================================================
            // ESECUZIONE DELLA RICERCA - I PARAMETRI DEVONO SELEZIONARE UN SOLO DOCUMENTO
            //=========================================================================

            // cripto la descrizione XML dei parametri
            abXmlUserSession = crypto.EncryptXML(_mUserSession, typeof(ArchivaASP_LOGIN), _mRsaServer);
            // cripto la index list XML dei parametri
            indexList.Indexes.Clear();
            indexList.Indexes.Add("CONTRATTI");
            abXmlIndexList = crypto.EncryptXML(indexList, typeof(ArchivaASP_INDEXLIST), _mRsaServer);

            // campi da visualizzare
            var fieldList = new ArchivaASP_FIELDLIST();
            fieldList.Fields.Add("IDENTIFICATIVO UNIVOCO");

            // TODO: Impostare qui i parametri di ricerca
            string parameters = "=(IDENTIFICATIVO UNIVOCO, #ZETH0000200005#)";

            // cripto la condizione XML dei parametri
            byte[] abXmlFieldCondition = crypto.EncryptXML(parameters, typeof(String), _mRsaServer);
            // cripto la lista dei campi fornibili XML dei parametri
            byte[] abXmlFieldName = crypto.EncryptXML(fieldList, typeof(ArchivaASP_FIELDLIST), _mRsaServer);

            byte[] abXmlResultNum;

            var lSearchError = _service.SearchDocument(abXmlUserSession, abXmlIndexList, abXmlFieldCondition, abXmlFieldName, out abXmlResultNum);

            //=========================================================================
            // LETTURA DEL CODICE DOCUMENTO CHE DEVE ESSERE APERTO
            //=========================================================================
            
            // numero di documenti per pagina
            const int nPageSize = 10;
            // numero di pagina
            const int nPageNum = 0;


            // campi risultanti
            var FieldResult = new ArchivaASP_FIELDVALUE();

            // cripto la descrizione XML dei parametri
            abXmlUserSession = crypto.EncryptXML(_mUserSession, typeof(ArchivaASP_LOGIN), _mRsaServer);
            // cripto la index list XML dei parametri
            byte[] abXmlPageNum = crypto.EncryptXML(nPageNum, typeof(Int32), _mRsaServer);
            // cripto la condizione XML dei parametri
            byte[] abXmlPageSize = crypto.EncryptXML(nPageSize, typeof(Int32), _mRsaServer);

            byte[] abXmlFieldResult;

            lSearchError = _service.GetSearchDocument(abXmlUserSession, abXmlPageNum, abXmlPageSize, out abXmlFieldResult);
            
            string sError = ""; 
            if (lSearchError != 0)
            {
                _service.GetErrorDescription(lSearchError, ref sError);
                string errore = sError;
            }

            // decripto le info 
            FieldResult = (ArchivaASP_FIELDVALUE)crypto.DecryptXML(abXmlFieldResult, typeof(ArchivaASP_FIELDVALUE), _mRsaClient);

            var codDoc = (string)((ArchivaASP_FIELDLIST)FieldResult.FieldValues[0]).Fields[0];

            //=========================================================================
            // LETTURA DOCUMENTO
            //=========================================================================

            byte[] abDocumentBuffer;

            // creo una index list con la lista selezionata
            indexList.Indexes.Clear();
            indexList.Indexes.Add("CONTRATTI");

            // cripto la descrizione XML dei parametri
            abXmlUserSession = crypto.EncryptXML(_mUserSession, typeof(ArchivaASP_LOGIN), _mRsaServer);
            // cripto il codice del documento in XML
            var abXmlDocumentCode = crypto.EncryptXML(codDoc, typeof(String), _mRsaServer);
            // cripto la index list XML dei parametri
            abXmlIndexList = crypto.EncryptXML(indexList, typeof(ArchivaASP_INDEXLIST), _mRsaServer);

            lSearchError = _service.GetDocument(abXmlUserSession, abXmlDocumentCode, out abDocumentBuffer);

            if (!_service.DocumentAccessibility(abXmlUserSession, abXmlDocumentCode))
                return;

            if (lSearchError != 0)
            {
                sError = "";
                _service.GetErrorDescription(lSearchError, ref sError);

                return;
            }
            // decripto le info 
            abDocumentBuffer = (byte[])crypto.DecryptXML(abDocumentBuffer, abDocumentBuffer.GetType(), _mRsaClient);

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(abDocumentBuffer);
            Response.End();
        }
Example #3
0
        public DocumentInfo GetDocument(string id, int idAzienda, TipoDocumentoArchiviazione tipoDocumento)
        {
            try
            {
                if (!string.IsNullOrEmpty(id))
                {
                    string anno = string.Empty;
                    if (id.Contains(" - "))
                        anno = id.Split('-')[2].Trim();
                    else if(id.Length >= 13)
                        anno = id.Substring(10, 4);
                    
                    id = id.Replace(" - ", "");

                    // ========================================================================================
                    // Recupero anno di riferimento --> ANNO SPEDIZIONE
                    // ========================================================================================
                    if (tipoDocumento == TipoDocumentoArchiviazione.FatturaPassiva)
                    {
                        var spesa = _spesaService.GetByIdentificativo(idAzienda, id);
                        if (spesa != null)
                            anno = spesa.AnnoArchiviazioneOttica.GetValueOrDefault().ToString();
                        else
                        {
                            var testataMovimento = _movimentiContabiliService.GetByIdentificativo(idAzienda, id);
                            if (testataMovimento != null)
                                anno = testataMovimento.AnnoArchiviazioneOttica.GetValueOrDefault().ToString();
                        }
                    }

                    var document = _documentService.GetDocument(id, idAzienda, tipoDocumento);
                    if (document.Body == null)
                    {
                        var impostazioni = _daoFactory.GetImpostazioniAziendaDao().GetByAzienda(idAzienda);
                        if (impostazioni != null &&
                            !string.IsNullOrEmpty(impostazioni.NomeApplicazioneArchiviazioneOttica) &&
                            !string.IsNullOrEmpty(impostazioni.UtenteArchiviazioneOttica) &&
                            !string.IsNullOrEmpty(impostazioni.PasswordArchiviazioneOttica) &&
                            !string.IsNullOrEmpty(impostazioni.IndiceContrattiArchiviazioneOttica))
                        {
                            _service = new ArchivaASP.ArchivaASP {Timeout = 60*60*10000};

                            long mRsaKeyLength = 0;

                            //=========================================================================
                            // OGGETTI PER CRITTOGRAFIA
                            //=========================================================================
                            // ----------------------------------------------------
                            //  Richiesta della chiave pubblica al server
                            // ----------------------------------------------------
                            var sKey = "";
                            if (_service.GetKey(ref sKey, ref mRsaKeyLength) != 0)
                            {
                                _log.ErrorFormat("Errore dal web service archiva - CHIAVE PUBBLICA DAL SERVER - {0} - application:{1} - user:{2} - password:{3} - id:{4}", Library.Utility.GetMethodDescription(), impostazioni.NomeApplicazioneArchiviazioneOttica, impostazioni.UtenteArchiviazioneOttica, impostazioni.PasswordArchiviazioneOttica, id);
                                return new DocumentInfo(new byte[0], id, null, null);
                            }

                            // creo il servizio RSA per il server
                            _mRsaServer = new RSACryptoServiceProvider((int) mRsaKeyLength);
                            var serverKey = (ArchivaASP_KEY) ArchivaASP_SERIALIZER.DeserializeObject(sKey, typeof (ArchivaASP_KEY));
                            var serverParameters = new RSAParameters {Exponent = serverKey.Exp, Modulus = serverKey.Mod};
                            _mRsaServer.ImportParameters(serverParameters);

                            //=========================================================================
                            // LOGIN
                            //=========================================================================

                            // creo il servizio RSA per il client
                            _mRsaClient = new RSACryptoServiceProvider((int) mRsaKeyLength);
                            var rsaParam = _mRsaClient.ExportParameters(false);
                            _mUserKey = new ArchivaASP_KEY {Mod = rsaParam.Modulus, Exp = rsaParam.Exponent};

                            // creo la struttura dei parametri
                            _mUserSession = new ArchivaASP_LOGIN
                            {
                                SessionId = 0,
                                Application = impostazioni.NomeApplicazioneArchiviazioneOttica,
                                User = impostazioni.UtenteArchiviazioneOttica,
                                Password = impostazioni.PasswordArchiviazioneOttica
                            };

                            // creo una index list vuota
                            var indexList = new ArchivaASP_INDEXLIST();

                            // Crypto service
                            var crypto = new ArchivaASP_CRY();

                            // cripto la descrizione XML dei parametri
                            var abXmlUserSession = crypto.EncryptXML(_mUserSession, _mUserSession.GetType(), _mRsaServer);
                            // cripto la chiave del client XML dei parametri
                            var abXmlUserKey = crypto.EncryptXML(_mUserKey, _mUserKey.GetType(), _mRsaServer);

                            byte[] abXmlIndexList;

                            // effettuo il login
                            var lLoginError = _service.Login(ref abXmlUserSession, abXmlUserKey, out abXmlIndexList);

                            // decripto le info di login
                            if (lLoginError == 0)
                            {
                                _mUserSession = (ArchivaASP_LOGIN)crypto.DecryptXML(abXmlUserSession, typeof(ArchivaASP_LOGIN), _mRsaClient);
                                indexList = (ArchivaASP_INDEXLIST)crypto.DecryptXML(abXmlIndexList, typeof(ArchivaASP_INDEXLIST), _mRsaClient);
                            }
                            else
                            {
                                _log.ErrorFormat("Errore dal web service archiva - LOGIN ERROR - {0} - application:{1} - user:{2} - password:{3} - id:{4}", Library.Utility.GetMethodDescription(), impostazioni.NomeApplicazioneArchiviazioneOttica, impostazioni.UtenteArchiviazioneOttica, impostazioni.PasswordArchiviazioneOttica, id);
                            }

                            //=========================================================================
                            // ESECUZIONE DELLA RICERCA - I PARAMETRI DEVONO SELEZIONARE UN SOLO DOCUMENTO
                            //=========================================================================

                            // ---------------------------------------------------
                            //  Parametri di Login - abXmlUserSession
                            // ---------------------------------------------------
                            // cripto la descrizione XML dei parametri
                            abXmlUserSession = crypto.EncryptXML(_mUserSession, typeof (ArchivaASP_LOGIN), _mRsaServer);

                            // ---------------------------------------------------
                            //  Lista degli indice selezionati - abXmlIndexList
                            // ---------------------------------------------------
                            // cripto la index list XML dei parametri
                            indexList.Indexes.Clear();
                            if (tipoDocumento == TipoDocumentoArchiviazione.Contratto)
                                indexList.Indexes.Add(impostazioni.IndiceContrattiArchiviazioneOttica);
                            else
                                // TODO: Calcolare l'anno corretto
                                indexList.Indexes.Add(impostazioni.IndiceFattureArchiviazioneOttica + " " + anno);
                            abXmlIndexList = crypto.EncryptXML(indexList, typeof (ArchivaASP_INDEXLIST), _mRsaServer);

                            // ---------------------------------------------------
                            //  Lista dei campi selezionati - abXmlFieldName
                            // ---------------------------------------------------
                            // campi da visualizzare
                            var fieldList = new ArchivaASP_FIELDLIST();
                            fieldList.Fields.Add("IDENTIFICATIVO UNIVOCO");

                            // cripto la lista dei campi fornibili XML dei parametri
                            var abXmlFieldName = crypto.EncryptXML(fieldList, typeof(ArchivaASP_FIELDLIST), _mRsaServer);

                            // ---------------------------------------------------
                            //  Condizioni di ricerca - abXmlFieldCondition
                            // ---------------------------------------------------
                            // TODO: Impostare qui i parametri di ricerca
                            var parameters = "=(IDENTIFICATIVO UNIVOCO, $" + id + "$)";

                            // cripto la condizione XML dei parametri
                            var abXmlFieldCondition = crypto.EncryptXML(parameters, typeof (String), _mRsaServer);

                            // ----------------------------------------------------------------
                            //  Esecuzione della ricerca ritorna direttamente il documento PDF
                            // ----------------------------------------------------------------
                            byte[] abDocumentBuffer;
                            var lSearchError = _service.SearchDocumentWithUniqueData(abXmlUserSession, abXmlIndexList, abXmlFieldCondition, abXmlFieldName, out abDocumentBuffer);
                            if (lSearchError != 0)
                            {
                                var sError = "";
                                _service.GetErrorDescription(lSearchError, ref sError);
                                
                                _log.ErrorFormat("Errore dal web service archiva - LETTURA DOCUMENTO - {0} - codice:{1} - descrizione:{2} - id:{3}", Library.Utility.GetMethodDescription(), lSearchError, sError, id);

                                _service.Logout(ref abXmlUserSession);

                                return new DocumentInfo(new byte[0], id, sError, null);
                            }

                            document = new DocumentInfo(abDocumentBuffer, id, "Archiva", ".pdf");
                            _documentService.SaveDocument(document.Body, id + ".pdf", ".pdf", id, idAzienda);

                            // Logout dal servizio
                            _service.Logout(ref abXmlUserSession);
                        }
                    }
                    else
                        document.FileName = "Archiva";

                    return document;
                }
                return new DocumentInfo(new byte[0], id, null, null);
            }
            catch(OutOfMemoryException ex)
            {
                _log.ErrorFormat("Errore inaspettato nel metodo - OUT OF MEMORY - {0} - id:{1}", ex, Library.Utility.GetMethodDescription(), id);
                return new DocumentInfo(new byte[0], id, "Il documento in questo momento non è disponibile di prega di riprovare più tardi", null);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("Errore inaspettato nel metodo - {0} - id:{1}", ex, Library.Utility.GetMethodDescription(), id);
                throw;
            }
        }