Wednesday, January 27, 2010

Ignore the Dot at Your Peril

Gmail ignores dots in emails. That's great if you want to track spam, use complex filtering, or just want to have a bit of fun. But it can wreak havoc on you and your customers if you aren't careful.

I started receiving emails from a very popular service for an account that didn't belong to me. The service, as so many do, uses email as a unique login identifier. I'm guessing the other user created an account and mistyped his email as mine, but with a different dot construction. The service determined the email was unique (as their backend datastore didn't already have it) and created the account. This could be a big problem for the service. Not only did I receive email about the services this user was provided, but I could have gone online and requested a new password.

Read that again: I can with no technical knowledge what-so-ever get someone else's billing info or purchasing choices from this service.

I called the service, explained the situation, and they disabled the other user's account. I'm dubious, however, that my explanation of Gmail's addressing choices will end up with the right audience.

The moral of the story: I don't know how many other email providers ignore dots (or other symbols or constructs) from emails, but if you're writing an application that relies on unique email you better have some robust checking going on during account creation.

Friday, January 8, 2010

Generate iTextSharp PDF Direct from Memory

If you need to dynamically generate a PDF using iTextSharp and you don't want to write to the disk, use the MemoryStream and PdfWriter.


using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class GetPDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
byte[] pdf = GetPdfDocument();

Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=MyDocument.pdf");
Response.OutputStream.Write(pdf, 0, pdf.Length);
}

private byte[] GetPdfDocument() {
byte[] result;

using (MemoryStream ms = new MemoryStream()) {
Document doc = new Document();
PdfWriter.GetInstance(doc, ms);
doc.AddTitle("Document Title");
doc.Open();
doc.Add(new Paragraph("My paragraph."));
doc.Close();
result = ms.GetBuffer();
}

return result;
}
}

FIPS Compliant PDF Generation with iTextSharp

When working with PDF documents using iTextSharp, be sure to use version 5.0.0 if your system is required to use FIPS compliant algorithms. Without it, you will likely get something like the following error:


System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
at System.Security.Cryptography.MD5CryptoServiceProvider..ctor()
at iTextSharp.text.pdf.PdfEncryption.CreateDocumentId()
at iTextSharp.text.pdf.PdfWriter.Close()
at iTextSharp.text.Document.Close()


If you're upgrading from version 4.1.6 and you use Document.HeaderFooter, you'll have to refactor your code as it has been removed.