Asp.net BBS source code analysis

Source: Internet
Author: User
ASP. Ne bbst source code analysis

1. login page

1. login page

On the login page, you need to pay attention to two items: How to Implement the verification code? How to Prevent SQL injection.

1. How to Implement the verification code?

The simple implementation of the verification code can be done by generating an image on the server side. Add an img html control at the front end and reload the checkcode page in the onclick event. The background uses the drawing in the DOTNET class library to generate an image.CodeThe Code is as follows:

style =" width: 73px; Height: 22px "align =" Left "/> </TD> 

Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> createcheckcodeimage (generatecheckcode ()); <br/>}</P> <p> private string generatecheckcode () <br/>{< br/> int number; <br/> char code; <br/> string checkcode = string. empty; </P> <p> random = new random (); </P> <p> for (INT I = 0; I <4; I ++) <br/> {<br/> Number = random. next (); </P> <p> code = (char) ('0' + (char) (Number % 10 )); </P> <p> checkcode + = code. tostring (); <br/>}</P> <p> response. cookies. add (New httpcookie ("checkcode", checkcode); </P> <p> return checkcode; <br/>}</P> <p> private void createcheckcodeimage (string checkcode) <br/>{< br/> If (checkcode = NULL | checkcode. trim () = string. empty) <br/> return; </P> <p> system. drawing. bitmap image = new system. drawing. bitmap (INT) math. ceiling (checkcode. length * 12.5), 22); <br/> graphics G = graphics. fromimage (image); </P> <p> try <br/> {<br/> // generate a random generator <br/> random = new random (); </P> <p> // clear the background color of the image <br/> G. clear (color. white); </P> <p> // specifies the background noise line of the image. <br/> for (INT I = 0; I <2; I ++) <br/>{< br/> int X1 = random. next (image. width); <br/> int X2 = random. next (image. width); <br/> int Y1 = random. next (image. height); <br/> int y2 = random. next (image. height); </P> <p> G. drawline (new pen (color. black), X1, Y1, X2, Y2); <br/>}</P> <p> Font font = new system. drawing. font ("Arial", 12, (system. drawing. fontstyle. bold); <br/> system. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true); <br/> G. drawstring (checkcode, Font, brush, 2, 2); </P> <p> // foreground noise of the picture <br/> for (INT I = 0; I <100; I ++) <br/>{< br/> int x = random. next (image. width); <br/> int y = random. next (image. height); </P> <p> image. setpixel (X, Y, color. fromargb (random. next (); <br/>}</P> <p> // draw the border line of the image <br/> G. drawrectangle (new pen (color. silver), 0, 0, image. width-1, image. height-1); </P> <p> system. io. memorystream MS = new system. io. memorystream (); <br/> image. save (MS, system. drawing. imaging. imageformat. GIF); <br/> response. clearcontent (); <br/> response. contenttype = "image/GIF"; <br/> response. binarywrite (Ms. toarray (); <br/>}< br/> finally <br/>{< br/> G. dispose (); <br/> image. dispose (); <br/>}< br/>

2. How to Avoid SQL injection during login verification?

There is an article on Microsoft's official websiteArticleTo prevent SQL injection, visit http://msdn.microsoft.com/en-us/library/ff648339.aspx. The main content is as follows:

2.1 input verification, never trust user input

Using system; <br/> using system. text. regularexpressions; </P> <p> Public void createnewuseraccount (string name, string password) <br/> {<br/> // check name contains only lower case or upper case letters, <br/> // the apostrophe, a dot, or white space. also check it is <br/> // between 1 and 40 characters long <br/> If (! RegEx. ismatch (useridtxt. text, @ "^ [A-Za-Z '. /s] {1, 40} $ ") <br/> throw new formatexception (" invalid name format "); </P> <p> // check password contains at least one digit, one lower case <br/> // letter, one uppercase letter, and is between 8 and 10 <br/> // characters long <br/> If (! RegEx. ismatch (passwordtxt. Text, <br/> @ "^ (? =. * \ D )(? =. * [A-Z]) (? =. * [A-Z]). {8, 10} $ ") <br/> throw new formatexception (" invalid password format "); </P> <p> // perform data access logic (using type safe parameters) <br/>... <br/>}

2.2 using storage terms

 Using system. data; <br/> using system. data. sqlclient; </P> <p> using (sqlconnection connection = new sqlconnection (connectionstring) <br/>{< br/> dataset userdataset = new dataset (); <br/> sqldataadapter mycommand = new sqldataadapter (<br/> "loginstoredprocedure", connection); <br/> mycommand. selectcommand. commandtype = commandtype. storedprocedure; <br/> mycommand. selectcommand. parameters. add ("@ au_id", sqldbtype. varchar, 11); <br/> mycommand. selectcommand. parameters ["@ au_id"]. value = SSN. text; </P> <p> mycommand. fill (userdataset); <br/>}< br/>

During execution, the value passed in @ au_id is plain text and does not cause SQL Injection problems. However, in this case, you must note how the original stored procedure is written. If it is the following stored procedure, it still cannot prevent injection.

Create procedure DBO. runquery <br/> @ var ntext <br/> as <br/> exec sp_executesql @ var <br/> go

2.3 use parameterized dynamic SQL statements (in the form of String concatenation)

Using system. data; <br/> using system. data. sqlclient; </P> <p> using (sqlconnection connection = new sqlconnection (connectionstring) <br/>{< br/> dataset userdataset = new dataset (); <br/> sqldataadapter mydataadapter = new sqldataadapter (<br/> "select au_lname, au_fname from authors where au_id = @ au_id", <br/> connection ); <br/> mycommand. selectcommand. parameters. add ("@ au_id", sqldbtype. varchar, 11); <br/> mycommand. selectcommand. parameters ["@ au_id"]. value = SSN. text; <br/> mydataadapter. fill (userdataset); <br/>}

2.4 database Permissions

If the database is queried, the permission is set a little lower

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.