A ASP.net authentication code class for encapsulation

Source: Internet
Author: User
Tags array object modify net string
The first reflection is whether these properties of the validation code can be set (that is, encapsulated into a class, then to expose the public properties and methods to use, or directly in the general process to achieve the validation code generation to output), the result is more unfortunate, is the latter ... Inside the algorithm feels pretty good, at least for me this kind of algorithm is almost not understand people, so think of to encapsulate her and then shamelessly into their class library to ^ ^
Old original file is as follows: Click to download

First, analyze the deficiencies in this code (what you think it is):
1, process-oriented programming, if you want to modify the properties of the verification code, such as the size of the font, background color and other details of things, you need to go to the general procedures to find the relevant code, modify it. However, this code is better, the generation of code string and generate a picture in different ways to separate, it is easy to find the place to modify.
2, if I want to apply this function to other places, such as the WinForm program, then the code is not done in a component (a DLL) in the form of reuse, but is copied and pasted and then modified ...
3, did not find the verification code generated after the Save up (-!) )

Of course, the so-called deficiency is relative, that is, there is time to think and then slowly design a verification code class will certainly be able to make a relatively good class out of the But if the project is doing and the project is very urgent, such code is actually more power, those so-called object-oriented design pattern code decoupling what is a cloud ~ ~~

Next is my revision of this code, first of all to determine what kind of effect I want, that is, after encapsulation I hope how to use him. First of all I would like to use only a new validation code instance (if necessary, you can set any of the properties of the Verification code), and then some methods that can call this instance are obtained in the form of string, stream, bitmap object, byte array (note, This verification code class does not include the ability to deposit the verification code into the context, and the individual feels that generating the verification code and saving the verification code to the context are two different functions, and it is not necessary to do this function to verify the code class. In this case, the code becomes very simple in the general handler, the first step is to create a CAPTCHA, and then call the method to get the code in the form of a string, and then save it to the afternoon, in the presence of the session or in the cookie as needed, the verification code class regardless of the And then call another method to get the captcha in the form of a byte array, which passes through the context. The Response.BinaryWrite () method can output the picture, which means that there are only three code in the general handler. The code that is invoked is roughly as follows:
Copy CodeThe code is as follows:
<summary>
Get validation code (generic handler entry function)
</summary>
<param name= "Contexts" > Current context </param>
public void ProcessRequest (HttpContext context)
{
Create a validation code
Validatecode Validatecode = new Validatecode ();
Gets the authentication code (string), writes the session
Context. session["Somevalidatecode"] = validatecode.getstring ();
Output Verification Code (picture)
Context. Response.BinaryWrite (Validatecode.getbytearray ());
}

The source code for the validation class (VS2010) is attached here, inside the provision of the property is not comprehensive, and the output of the picture in BMP format output, than the original code output to clear a lot of, to a certain extent, the function of encapsulation but not necessarily packaged very well, for the exchange of learning with: Click to download new

PS: Remember just out of work when the code is very poor (worse than now), generally get a piece of code is directly used, never thought about whether to modify the code or encapsulation, then my master taught a very deep impression. Personally feel that there is nothing wrong with other people's code, one person has used at least to prove the availability of the code, the other is limited IQ, some things they want to break the scalp is not other people think well, but with someone else's code must not blindly take the use of, preferably according to their actual situation to make the necessary modifications or encapsulation, Even if it's simple, it's just a separate layer. Of course, or that sentence, if the project is urgent to do this is obviously a waste of time, in short, do not think, take the use, at least to see the key code or the overall structure of the code to find out.

The script house is packaged directly. The old folder is above, and the following is the contents of the new folder. Download Address/201012/yuanma/yanzhengma.rar

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.