Simple. NET website authorization solution based on HttpModule

Source: Internet
Author: User

.

1. Create a class library project named LYB. NET. Authorize. Add the following two references:

Change the target Framework in the project attribute to. Net Framework 4.0 instead of the default. Net Framework 4.0 client profile. That is, the full version of. NET is used instead of the lite version.

2. added the IAuthorize interface, which provides a read-only UniqueID and a method to verify the registration code. As follows:

 

                        UniqueID {                             VerifyKey(

3. Add a MACAuthorize class and use a MAC address to implement the above interface (of course, you can also customize other classes and use hard disk serial numbers ):

   =  ManagementClass(= (ManagementObject mo  (()mo[] == = mo[  {   VerifyKey(=  (alg.GetKey(_strId) ==

4. In the above MACAuthorize verification, it is used for an Algorithm class, which is an Algorithm class for generating registration codes. This class is not only used for verification, but when we create a registration machine, this class is also used. We add and implement it:

                      GetKey( strResult = strUniqueId.Replace(, [] resources = ( i = ; i < resources.Length; i++ (resources[i] <=  && resources[i] >= += 

5. Finally, we need to customize an HttpModule to intercept http requests and force Verification:

    +=  context_BeginRequest(= = strKey = ConfigurationManager.AppSettings[                +

6. After completing the above steps, you can package and generate a DLL. Add it to your WEB site reference. Modify WEB. CONFIG:

A. Add the appSettings node: (this is the place where the registration code is stored)

 

B. Add the following to the System. Web node: (this is to add the custom httpmodule)

              

7. When you run your website, the following effects can be displayed:

8. The last step is to create a new WINFORM Project (you can create any project based on your interests). The interface is as follows (form1 above and form2 below ):

Add a reference to LYB. NET. Authorize. DLL and add the following code to the BUTTON:

  button1_Click( (= .textBox2.Text =

Run the command and COPY the machine code on your site to generate a registration code!

Finally, copy the generated registration code to the value of web. config <add key = "LYB. NET. AUTHORIZE" value = ""/>, that is, the authorization is successful. Your website can run normally.

In fact, this solution mainly discusses a usage of HttpModule. This method is quite weak when creating a large-scale commercial WEB framework. If you are interested, you can study it in depth.

 

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.