2

I am making a web application that when user registered would randomly create an activation code and put it in a database. The web application would then send it in an email to the user. How would Ido this? and what sql server data type would I use? I want the activation code to be no longer than 12 characters of number. please can any one help me

Francisco Alvarado
  • 2,815
  • 2
  • 26
  • 51
shimaa
  • 41
  • 1
  • 3

1 Answers1

0

Probably the most common approach would be to use a Guid. It won't fit in 12 characters though, base 64 it would be 22 characters, which doesn't meet your needs. If you really need it short what I'd probably do is base64 encode a new Guid, and simply take the left 12 characters, then check that it is unique.

var guid = Guid.NewId();
string str = ConvertToBase64(guid.ToByteArray());
return str.Substr(0, 12);

Note, you must ensure that this code is unique by searching for it in your database. The range is to small to ensure uniqueness. Probably you'd be better off simply relaxing your requirement on 12 characters, but I don't know your app.

Fred Thomas
  • 934
  • 3
  • 10
  • 16
  • Rather than using base64, read the following article ... http://stackoverflow.com/questions/1278934/shortest-encoding-for-guid-for-use-in-a-url – JTew May 08 '11 at 04:11