Iis6 simple random code example, iis6 code example

Source: Internet
Author: User

Iis6 simple random code example, iis6 code example
Solve the problem that the Random Code cannot be displayed on the win2003 iis6 Server

 

Display random code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "login. aspx. cs" Inherits = "TestDemo. login" %> <! DOCTYPE html> 

 



// Load the event

protected void Page_Load(object sender, EventArgs e)        {            if (!this.IsPostBack)            {                this.ViewState["GUID"] = System.Guid.NewGuid().ToString();                this.lblGUID.Text = this.ViewState["GUID"].ToString();            }        }

 

// Blank page with Random Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Vcode.aspx.cs" Inherits="TestDemo.Vcode" %>

 

Background
Using System; using System. collections. generic; using System. drawing; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace TestDemo {public partial class Vcode: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string checkCode = GetRandomCode (4); Session ["CheckCode"] = checkCode; SetPageNoCache (); CreateImage (checkCode );} /// <su Mmary> // set that the page is not cached /// </summary> private void SetPageNoCache () {Response. buffer = true; Response. expiresAbsolute = System. dateTime. now. addSeconds (-1); Response. expires = 0; Response. cacheControl = "no-cache"; Response. appendHeader ("Pragma", "No-Cache");} private string CreateRandomCode (int codeCount) {string allChar = ", A, B, c, D, E, F, G, H, I, J, K, M, N, P, Q, R, S, T, U, W, X, Y, Z "; string [] AllCharArray = allChar. split (','); string randomCode = ""; int temp =-1; Random rand = new Random (); for (int I = 0; I <codeCount; I ++) {if (temp! =-1) {rand = new Random (I * temp * (int) DateTime. now. ticks);} int t = rand. next (35); if (temp = t) {return CreateRandomCode (codeCount); // performance problem} temp = t; randomCode + = allCharArray [t];} return randomCode;} private string GetRandomCode (int CodeCount) {string allChar = ", A, B, C, D, E, F, G, h, I, J, K, M, N, P, Q, R, S, T, U, W, X, Y, Z "; string [] allCharArray = allChar. split (','); string Ran DomCode = ""; int temp =-1; Random rand = new Random (); for (int I = 0; I <CodeCount; I ++) {if (temp! =-1) {rand = new Random (temp * I * (int) DateTime. now. ticks);} int t = rand. next (33); while (temp = t) {t = rand. next (33) ;}temp = t; RandomCode + = allCharArray [t];} return RandomCode;} private void CreateImage (string checkCode) {int iwidth = (int) (checkCode. length * 14); System. drawing. bitmap image = new System. drawing. bitmap (iwidth, 20); Graphics g = Graphics. fromImage (image); Font f = new System. drawing. font ("Arial", 10); //, System. drawing. fontStyle. bold); Brush B = new System. drawing. solidBrush (Color. black); Brush r = new System. drawing. solidBrush (Color. fromArgb (166, 8, 8); // g. fillRectangle (new System. drawing. solidBrush (Color. blue), 0, 0, image. width, image. height); // g. clear (Color. aliceBlue); // background color g. clear (System. drawing. colorTranslator. fromHtml ("# 99C1CB"); // background color char [] ch = checkCode. toCharArray (); for (int I = 0; I <ch. length; I ++) {if (ch [I]> = '0' & ch [I] <= '9') {// The number is displayed in red. drawString (ch [I]. toString (), f, r, 3 + (I * 12), 3);} else {// The letter is displayed in black. drawString (ch [I]. toString (), f, B, 3 + (I * 12), 3 );}} // for Loop is used to generate some random horizontal lines // Pen blackPen = new Pen (Color. black, 0); // Random rand = new Random (); // for (int I = 0; I <5; I ++) // {// int y = rand. next (image. height); // g. drawLine (blackPen, 0, y, image. width, y); //} System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. jpeg); // history back does not repeat Response. cache. setNoStore (); // This sentence Response. clearContent (); Response. contentType = "image/Jpeg"; Response. binaryWrite (ms. toArray (); g. dispose (); image. dispose ();}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.