License.cs :  » Development » devAdvantage » AnticipatingMinds » PlatformServices » Licensing » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » devAdvantage 
devAdvantage » AnticipatingMinds » PlatformServices » Licensing » License.cs
using System;
using System.Xml;
using System.IO;

namespace AnticipatingMinds.PlatformServices.Licensing{
  public enum LicenseType
  {
    Unknown,
    RegistrationInformationLock,
    TimeExpiration
  }

  [Flags]
  public enum LicensingState
  {
    LicenseNotFound = 0x01,
    InvalidLicenseSignature = 0x02,
    LicensedProductIdMistmatch = 0x04,
    LicensedProductVersionMistmatch = 0x08,
    LicenseHasExpired = 0x20,
    LicenseIsValid = 0x40
  }

  /// <summary>
  /// Summary description for License.
  /// </summary>
  public sealed class License
  {
    internal License(XmlElement licenseElement,LicenseStore licenseStore)
    {
      if(licenseElement == null)
        throw new ArgumentNullException("licenseElement");
      if(licenseStore == null)
        throw new ArgumentNullException("licenseStore");

      this.licenseElement = licenseElement;
      this.licenseStore = licenseStore;

      if(licenseElement["LicenseType"].InnerText == "RegistrationInformationLock")
        licenseType = LicenseType.RegistrationInformationLock;

      if(licenseElement["LicenseType"].InnerText == "TimeExpiration")
        licenseType = LicenseType.TimeExpiration;
    }

    public LicenseType LicenseType
    {
      get
      {
        return licenseType;
      }
    }

    public string LicenseId
    {
      get
      {
        return GetStringValue("LicenseId");
      }
    }

    public string ProductId
    {
      get
      {
        return GetStringValue("LicensedProductId");
      }
    }

    public string ProductName
    {
      get
      {
        return GetStringValue("LicensedProductName");
      }
    }

    public Version LicensedVersion
    {
      get
      {
        XmlElement licensedVersion = licenseElement["LicensedProductVersion"];
        if(licensedVersion == null)
          throw new LicenseFormatException("LicensedProductVersion element is missing");
        
        return new Version(licensedVersion.InnerText);
      }
    }

    public string GetStringValue(string valueElementName)
    {
      if(licenseElement.SelectSingleNode(valueElementName) == null)
        return string.Empty;

      return licenseElement.SelectSingleNode(valueElementName).InnerText;
    }

    public bool IsValid(string productId,Version productVersion)
    {
      return GetLicenseStatus(productId,productVersion) == LicensingState.LicenseIsValid;
    }

    public void ValidateSignature()
    {
      //Check license integrity
      if(!licenseStore.IsLicenseSignatureValid(licenseElement))
        throw new LicenseSignatureMismatchException(this);
    }

    public LicensingState GetLicenseStatus(string productId,Version productVersion)
    {
      //Check license integrity
      if(!licenseStore.IsLicenseSignatureValid(licenseElement))
        return LicensingState.InvalidLicenseSignature;

      //Check common license properties
      if(productId != ProductId)
        return LicensingState.LicensedProductIdMistmatch;
        
      if(!IsLicensedVersion(productVersion))
        return LicensingState.LicensedProductVersionMistmatch;

      //Check expiration Date if exists
      if(licenseElement["ExpirationDate"] != null)
      {
        DateTime expirationDate = DateTime.Parse(licenseElement["ExpirationDate"].InnerText);
        if(DateTime.Now > expirationDate)
          return LicensingState.LicenseHasExpired;
      }

      return LicensingState.LicenseIsValid;
    }
    public void Validate(string productId,Version productVersion)
    {
      switch(GetLicenseStatus(productId,productVersion))
      {
        case LicensingState.InvalidLicenseSignature:
          throw new LicenseSignatureMismatchException(this);

        case LicensingState.LicensedProductIdMistmatch:
          throw new LicenseProductIdMismatchException(this);

        case LicensingState.LicensedProductVersionMistmatch:
          throw new LicenseProductVersionMismatchException(this);

        case LicensingState.LicenseHasExpired:
          throw new LicenseExpiredException(this);
      }
    }

    internal XmlElement GetLicenseElement()
    {
      return licenseElement;
    }

    private bool IsLicensedVersion(Version ProductVersion)
    {
      if(ProductVersion.Major == LicensedVersion.Major && 
        ProductVersion.Minor == LicensedVersion.Minor && 
        ProductVersion.Build <= LicensedVersion.Build && 
        ProductVersion.Revision <= LicensedVersion.Revision)
        return true;
      else
        return false;
    }

    private XmlElement licenseElement;
    private LicenseStore licenseStore;
    private LicenseType licenseType = LicenseType.Unknown;
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.