[Coding For Fun] Do you have a running license?

Source: Internet
Author: User

This document describes how to add a running license to a program.

First look at the effect (Declaration, this is not a simple output of a string ):

This article will involve three classes: License, LicenseProvider, LicenseManager, and registry class.

First, create a License for the program.

   1: public class SimpleRuntimeLicense : License
   2: {
   3:     private string TypeCode;
   4:     public override void Dispose() {}
   5:  
   6:     public SimpleRuntimeLicense(Type type)
   7:     {
   8:         TypeCode = type.GUID.ToString();
   9:     }
  10:  
  11:     public override string LicenseKey
  12:     {
  13:         get { return TypeCode; }
  14:     }
  15: }

Then, it is worth noting that the execution of the License is using the common Provider mode in. Net.

   1: public class SimpleLicenseProvider : LicenseProvider
   2: {
   3:  
   4:    public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
   5:    {
   6:        if (context.UsageMode == LicenseUsageMode.Runtime)
   7:        {
   8:            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\MySimplceLicense");
   9:  
  10:            if (null != key)
  11:            {
  12:                string strlic = key.GetValue("SN").ToString();
  13:                if (strlic != null)
  14:                {
  15:                    if (string.Compare(strlic, type.GUID.ToString(), false) == 0)
  16:                        return new SimpleRuntimeLicense(type);
  17:                }
  18:            }
19: if (allowExceptions) throw new LicenseException (type, instance, "You have no running license! :)");
  20:            
  21:        }
  22:  
  23:        return null;
  24:    }
  25: }

In this way, we have completed the basic structure of the License, and then we need to call its class to add the corresponding mark. We will take the MyClass to open the knife.

   1: [GuidAttribute("7F46DB6D-98CD-4cb7-BA95-014F678B2375")]
   2: [LicenseProvider(typeof(SimpleLicenseProvider))]
   3: public class MyClass
   4: {
   5:     public void Validate()
   6:     {
   7:         LicenseManager.Validate(typeof(MyClass), this);
   8:     }
   9: }

In this way, in the future, we only need to use the Validate method to verify the License first. This is very useful for protecting some key methods and classes.

   1: try
   2: {
   3:     MyClass c = new MyClass();
   4:     c.Validate();
   5: }
   6: catch (Exception ex)
   7: {
   8:     Console.WriteLine(ex.Message);
   9: }

In this way, the initial result is displayed. Do you have any associations? have fun...

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.