On the Silverlight login page, it is often used to enter the verification code. How to generate the verification code is similar to that generated by. net. The following is a brief introduction:
1. Create an indentifycodeclass. CS file under the Silverlight Project
1) First, pay attention to adding references. Using system. Windows. Media. imaging;
2) Add a method to generate a verification code
Public String Createindentifycode (INT count)
{
String allchar = "1, 2, 3, 4, 5, 6, 7, 8, 9, 0, a, a, B, B, C, C, D, D, E, E, F, F," +
"G, G, H, H, I, I, J, J, K, k, l, l, m, M, N, N, O, O, P, p, Q, Q, R, R, S, S, "+
"T, t, u, u, v, V, W, W, X, X, Y, Y, Z ";
String [] allchararray = allchar. Split (',');
String randomcode = "";
Int temp =-1;
Random Rand = new random ();
For (INT I = 0; I <count; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (61 );
If (temp = T)
{
Return createindentifycode (count );
}
Temp = T;
Randomcode + = allchararray [T];
}
Return randomcode;
}
3) Add a method to generate the background noise image of the verification code.
Random r = new random (datetime. Now. millisecond );
Public void Creatimage (String text, image imgsource, int IW, int IH)
{
Grid GX = new grid ();
Canvas cv1 = new canvas ();
For (INT I = 0; I <6; I ++)
{
Polyline P = new polyline ();
For (int ix = 0; ix <R. Next (3, 6); ix ++)
{
P. Points. Add (new point (R. nextdouble () * IW,
R. nextdouble () * IH ));
}
Byte [] buffer = new byte [3];
R. nextbytes (buffer );
Solidcolorbrush SC = new solidcolorbrush (color. fromargb (255,
Buffer [0], buffer [1], buffer [2]);
P. Stroke = SC;
P. strokethickness = 0.5;
Cv1.children. Add (P );
}
Canvas cv2 = new canvas ();
Int y = 0;
Int LW = 6;
Double W = (IW-LW)/text. length;
Int H = (INT) IH;
Foreach (char X in text)
{
Byte [] buffer = new byte [3];
R. nextbytes (buffer );
Solidcolorbrush SC = new solidcolorbrush (color. fromargb (255,
Buffer [0], buffer [1], buffer [2]);
Textblock T = new textblock ();
T. textalignment = textalignment. Center;
T. fontsize = R. Next (H-3, H );
T. Foreground = SC;
T. Text = x. tostring ();
T. Projection = new planeprojection ()
{
Rotationx = R. Next (-30, 30 ),
Rotationy = R. Next (-30, 30 ),
Rotationz = R. Next (-10, 10)
};
Cv2.children. Add (t );
Canvas. setleft (T, LW/2 + y * w );
Canvas. settop (T, 0 );
Y ++;
}
GX. Children. Add (cv1 );
GX. Children. Add (cv2 );
Writeablebitmap W = new writeablebitmap (GX, new transformgroup ());
W. Render (GX, new transformgroup ());
Imgsource. Source = W;
}
2. Add the following to the usercontrol_loaded event on the logon page: Code You can:
Indentifycodeclass code = new indentifycodeclass ();
String generatedcode = code. createindentifycode (6 );
Code. creatimage (generatedcode, checkcodeimage, 150, 30 );
3. Obtain the verification code entered by the user in the text box and compare it with the generated verification code.
....
String indentifycode = This. tbindentifycode. Text. Trim ();
If (generatedcode! = Indentifycode)
{
MessageBox. Show ("Incorrect verification code! ");
This. tbindentifycode. Focus (); // input focus
}
4. implement verification results. Just try it!