martedì, dicembre 11, 2007

Send smime message from NET

Using the NET Framowork classes is not possible to send correct smime pkcs7 messages. In particular, SmtpClient class adds own headers before sending message. The result is a not valid smime message. See bug on connect.microsoft.com and some posts on newsgroup: this and this.

If you are curious you can check how SmtpClient works, using Reflector. You will found that SmtpClient adds those headers. Grrrr!!!

Waiting for a solution from MS, at the moment the only solution is to use a thirdparty component or some free open source Smtp class. Using Google you can found them. I choosed Axosoft classes.
Well, they are very simple and some minor bugs, but they are very usefull to do some tests.

The very first think to do is to add a new message type in the MessageType enum and the Smtp.SendEmail method:

case MessageType.PKCS7:
WriteBuffer(ns, "Content-Type: application/pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\"" + "\r\n");
WriteBuffer(ns, "Content-Disposition: attachment; filename=\"smime.p7m\"" + "\r\n");
WriteBuffer(ns, "Content-Transfer-Encoding: base64" + "\r\n");
WriteBuffer(ns, "\r\n" + msg.EmailMessage + "\r\n.\r\n");
break;

The new MessageType.PKCS7 inform the Smtp.SendEmail method to insert the required header for smime message.



You can create a smime message in few simple steps:
- get the bytes of the message
- encode them as pkcs7 message using a certificate
- send the message using the new MessageType



string inFile = "data.txt";
byte[] origData = File.ReadAllBytes(inFile);
ContentInfo ci = new ContentInfo(origData);

X509Certificate2 cert = CertUtility.GetCertFromStore("Fabrizio", StoreName.My);

SignedCms signedCms = new SignedCms(contentInfo);
CmsSigner cmsSigner = new CmsSigner(cert);
signedCms.ComputeSignature(cmsSigner);
byte[] encodedData = signedCms.Encode();

Axosoft.Common.Utilities.Smtp smtpAx = new Axosoft.Common.Utilities.Smtp();
smtpAx.SmtpServer = "smtpserver";
Axosoft.Common.Utilities.MailMessage msgAx = new Axosoft.Common.Utilities.MailMessage();
msgAx.EmailFrom = "foo@foo";
msgAx.AddEmailTo("dest@dest");
msgAx.EmailSubject = "TEST " + DateTime.Now.ToString();
msgAx.EmailMessageType = Axosoft.Common.Utilities.MessageType.PKCS7;
msgAx.EmailMessage = Convert.ToBase64String(encodedData);
smtpAx.SendEmail(msgAx);

martedì, novembre 27, 2007

Service install error

Using VS2005 you can create NTServices. Every service runs under the permission of an account, tipically Network Service or Local Service. But there are situations where you need to run the service with another account.
When you create an instance of a ServiceProcessInstaller you can specifiy the Account property: LocalService, NetworkService, LocalSystem or User. User is not a real account. It's a "placeholder" and you will set the real account during installation.

To install a service, you must use InstallUtil.exe from the NET Framework:

c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ InstallUtil.exe foo.exe

After pressing enter, install starts and after some seconds you are prompted with a windows asking username and password for the account:



Important: you must add ".\" before the username. If you do not that, you get an error and your service will not be installed:

An exception occurred during the Install phase.
System.ComponentModel.Win32Exception: The account name is invalid or does not exist, or the password is invalid for the account name specified



lunedì, novembre 19, 2007

NLB does not check IIS status

Rember: Network Load Balancing (NLB) does not check services status. It works directly on TCP/IP and it is not aware of upper services status (IIS, etc.).

If you have a NLB system running and you shutdown a machine, the "cluster" converges in some seconds. The http connections are than served by the remaining machines. But if you stops the IIS instance of one server, NLB continues to send http traffic to that machine.

Reference: http://support.microsoft.com/?id=234151



PS: I do not agree with calling NLB a "cluster" solution. Windows 2003 offers a real cluster solution. NLB is only a "light cluster".

domenica, novembre 04, 2007

FTPS su IIS7

La nuova versione server di Windows (Windows 2008) avrà un supporto nativo per FTPS (da non confondersi con SFTP).
Attualmente Win2008 è scaricabile dal sito di Microsoft in versione RC0 - Release Candidate 0 (la data prevista per il rilascio definitivo è il 27 Febbraio 2008).
Win2008 contiene il nuovo IIS, versione 7. IIS7 supporta siti FTPS (FTP over TLS). Ma attenzione, IIS7 presente nella RC0 è ancora del vecchio tipo, per intenderci quello di IIS6.
Il nuovo servizio FTP/FTPS deve essere scaricato a parte ed installato dopo aver installato IIS7.
[download FTP/FTPS service per IIS7]
Sempre sul sito IIS.NET è presente un interessante articolo "Using FTP over SSL" che spiega come installare e configurare il servizio FTP. Spiega anche come autocostruirsi un certificato di test per provare FTPS.

Uno degli aspetti più importanti di FTPS è la possibilità di impostare quali connessioni debbano essere cifrate: solo quelle del canale di controllo (username/password/comandi), solo per il cancale dati o entrambi.


Dopo l'installazione molto probabilmente non sarà possibile eseguire connessioni da altri host. E' "solo" colpa di Windows Firewall presente su Windows 2008. Se l'obiettivo è fare solo un paio di test sarà sufficiente disabilitarlo. Se invece lo si vuole mantenere attivo, è necessario attivare le regole del caso.


Per sviluppatori: al momento non ho ancora trovato la documentazione, ma sembra sia possibile scivere propri moduli di autenticazione per FTP su IIS. Vedi:
How to write a simple Custom Authentication Provider for FTP 7
Effettivamente nelle finestre di management di IIS ci sono alcune voci e finestre interessanti:





SQL Server - DBCC DROPCLEANBUFFERS

Per essere sicuri di eseguire dei test di performane realmente indicativi, ricordarsi di svuotare i buffer di SQL Server con il comando DBCC DROPCLEANBUFFERS
In questo modo i tempo i tempi di esecuzione non saranno influenzati dalle esecuzioni precedenti delle stesse query.

giovedì, novembre 01, 2007

Redbook - TCP/IP Tutorial and Technical Overview

Da leggere o da tenere come riferimento. Un bel Redbook di IBM uscito un po' di anni addietro e aggiornato nel 2006: TCP/IP Tutorial and Technical Overview

C'è un po' tutto quello che ha a che fare con TCP/IP: da IP stesso fino ai protocolli di più alto livello (http, ftp, smtp, ldap, h323, ecc.).

giovedì, ottobre 18, 2007

lunedì, settembre 03, 2007

CMTrading

Dal 3 settembre 2007 lavoro in CMTrading




giovedì, agosto 02, 2007

Creazione di immagini con testo ruotato in Asp.Net

In alcuni casi sarebbe utile poter costruire in html tabelle con i nomi delle colonne ruotate di 90°...

http://docs.google.com/View?docid=dchmct9k_134gcz96s

giovedì, giugno 07, 2007

How to insert a 2D DataMatrix in a pdf using IEC16022Sharp and PdfLib

A very simple example:


using System;
using System.Collections.Generic;
using System.Text;
using PDFlib_dotnet;

using IEC16022Sharp;

namespace PdfLib7_tests
{
class Program
{
static void Main(string[] args)
{
Test_Iec16022Sharp_PdfLib();
}

private static void Test_Iec16022Sharp_PdfLib()
{
PDFlib lib = new PDFlib();
lib.begin_document("out.pdf", "");
lib.begin_page_ext(595, 842, "");

DataMatrix dm = new DataMatrix("Test using IEC16022Sharp and PdfLib");
string virtualFileName = "/pvf/image/myTestImage.bmp";
lib.create_pvf(virtualFileName, dm.FastBmp.ToByteArray(), "");
int imgID = lib.load_image("auto", virtualFileName, "");
lib.place_image(imgID, 100, 600, 1);
lib.delete_pvf(virtualFileName);

lib.end_page_ext("");
lib.end_document("");
}
}
}


IEC16022Sharp: http://sourceforge.net/projects/iec16022sharp/

PdfLib: http://www.pdflib.com

mercoledì, aprile 04, 2007

martedì, marzo 06, 2007

Visual Studio 2005: query builder

I added a new empty DataSet in my project. Than I added a new DataTable+TableAdapter.

This is my query:

SELECT Oggetti.IDOggetto, Oggetti.IDPersona, Oggetti.NomeOggetto
FROM Oggetti INNER JOIN Person ON Oggetti.IDPersona = Person.ID
WHERE (@Name IS NULL OR Person.Name=@Name) AND
(@City IS NULL OR Person.City=@City) AND
(@Num IS NULL OR Person.Num=@Num)

When I entered it in the query builder form, it was "rewritten" in this way:

SELECT Oggetti.IDOggetto, Oggetti.IDPersona, Oggetti.NomeOggetto
FROM Oggetti INNER JOIN Person ON Oggetti.IDPersona = Person.ID
WHERE (@Name IS NULL) AND (@City IS NULL) AND (@Num IS NULL) OR
(@Name IS NULL) AND (@Num IS NULL) AND (Person.City = @City) OR
(@City IS NULL) AND (@Num IS NULL) AND (Person.Name = @Name) OR
(@Num IS NULL) AND (Person.City = @City) AND (Person.Name = @Name) OR
(@Name IS NULL) AND (@City IS NULL) AND (Person.Num = @Num) OR
(@Name IS NULL) AND (Person.City = @City) AND (Person.Num = @Num) OR
(@City IS NULL) AND (Person.Name = @Name) AND (Person.Num = @Num) OR
(Person.City = @City) AND (Person.Name = @Name) AND (Person.Num = @Num)




The question is: WHY???


In the particular case I have found a solution but I don't know if it applies also to other cases.
The solution is to write the WHERE condition a bit different. The new query is:

SELECT Oggetti.IDOggetto, Oggetti.IDPersona, Oggetti.NomeOggetto
FROM Oggetti INNER JOIN Person ON Oggetti.IDPersona = Person.ID
WHERE (@Name IS NULL OR @Name=Person.Name) AND
(@City IS NULL OR @City=Person.City) AND
(@Num IS NULL OR @Num=Person.Num)






The diffence is "@Name=Person.Name" instead of "Person.Name=@Name" (and the same with City and Num).

(@Name IS NULL OR Person.Name=@Name) AND
(@City IS NULL OR Person.City=@City) AND
(@Num IS NULL OR Person.Num=@Num)


(@Name IS NULL OR @Name = Person.Name) AND
(@City IS NULL OR @City = Person.City) AND
(@Num IS NULL OR @Num = Person.Num)



Why this behaviour ??? Any suggestions?



venerdì, gennaio 19, 2007

IEC16022Sharp 0.3.5.0

New release of IEC16022sharp 0.3.5.0

http://sourceforge.net/projects/iec16022sharp/


I have added a new class for very fast creation of 1 bit/pixel Bmp file.

mercoledì, gennaio 17, 2007

How to insert a 2D DataMatrix in a pdf using IEC16022Sharp and iTextSharp

A very simple example:

Document doc = new Document(new Rectangle(0, 0, 595, 842));
FileStream os = new FileStream("DataMatrixout.pdf", FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(doc, os);
doc.Open();
doc.NewPage();
doc.Add(new Paragraph("IEC16022Sharp simple test"));
DataMatrix dm = new DataMatrix("This is a test with iTextSharp and Iec16022Sharp");

// Add image to document
Image img1 = Image.GetInstance(dm.Image, ImageFormat.Bmp);
doc.Add(img1);

// Add image to Pdf direct content
Image img2 = Image.GetInstance(dm.Image, ImageFormat.Bmp);
img2.SetAbsolutePosition(300, 700);
writer.DirectContent.AddImage(img2);

doc.Close();




http://sourceforge.net/projects/iec16022sharp/
http://itextsharp.sourceforge.net/

martedì, gennaio 16, 2007

IEC16022Sharp

I have published the first public realease of Iec16022Sharp, a C# library for generating 2D Data Matrix barcode.



The core of the library is a C# porting of an existing ansi C code currently maintained by Stefan Schmidt (http://www.datenfreihafen.org/projects/iec16022.html).

giovedì, gennaio 11, 2007

Esperimenti con System.Transaction, Typed DataSet e MSMQ

In questi giorni sto facendo un po' di esperimenti con i Typed DataSet in NET 2.0. Tutto molto interessante e potente ma...

http://docs.google.com/View?docid=dchmct9k_19fqnbz5

[Typed DataSet, TransactionScope, TableAdpter & C.]

lunedì, gennaio 08, 2007

Intellidoc


Dal 1 Gennaio 2007 lavoro in Intellidoc srl
www.intellidoc.it