This sample shows how to generate and save an XML license to a MemoryStream.
XmlLicense license =newXmlLicense();license.Id=IdGenerator.Create("lic",5);license.Product=newProduct();license.Product.Id=IdGenerator.Create("prd",5);// Create and sign a licenseXmlLicenseBuilder builder =newXmlLicenseBuilder();builder.SignatureProvider=RSASignature.CreateFromKeyFile("KeyPair.snk");builder.AddOrUpdate(license);builder.Sign();// Save the license MemoryStream stream =newMemoryStream();builder.Save(stream);
Validates the current license signature. This method doesn't validate license restrictions. Is not meant to be used to validate the license in client applications. To validate the license in client applications use the class.
This code creates an XML license with a custom restriction.
// Create a digital signerRSASignature signer =RSASignature.CreateFromKeyFile("Keys.snk");// Minimum hardware requirements// Total Memory 4 Gb, Two Cpuint totalMemory =4096;int cpuCount =2;var license =newXmlLicense() .WithUniqueId() .ForProduct("Product","1.0.0.0") .LicensedTo("ACME","devs@acme.com") .WithRestrictions(newMinimumRequirementsRestriction(totalMemory, cpuCount)) .SignWith(signer);license.Save("Product.licenses");
' Create a digital signer
Dim signer As RSASignature = RSASignature.CreateFromKeyFile("Keys.snk")
' Minimum hardware requirements
' Total Memory 4 Gb, Two Cpu
Dim totalMemory As Integer = 4096
Dim cpuCount As Integer = 2
Dim license = New XmlLicense() _
.WithUniqueId() _
.ForProduct("Product", "1.0.0.0") _
.LicensedTo("ACME", "devs@acme.com") _
.WithRestrictions(New MinimumRequirementsRestriction(totalMemory, cpuCount)) _
.SignWith(signer)
license.Save("Product.licenses")
To validate a custom restriction, handle the CreateCustomRestriction event and create the proper restriction object.
Dim manager As New XmlLicenseManager()
AddHandler manager.CreateCustomRestriction, Sub(s, e)
If e.RestrictionName = "MinimumRequirements" Then
e.Restriction = New MinimumRequirementsRestriction()
End If
End Sub
Dim license As XmlLicense = manager.Validate(File.ReadAllText("Product.licenses"))
This is the implementation of a custom restriction that is able to validate the machine minimum requirements in terms of available memory and total number of CPU present.
Public Class MinimumRequirementsRestriction
Inherits Restriction
Implements IXmlSerializable
Private _totalMemory As Long
Private _processorCount As Int32
Public Overrides ReadOnly Property Name As String
Get
Return "MinimumRequirements"
End Get
End Property
Public ReadOnly Property TotalMemory As Long
Get
Return _totalMemory
End Get
End Property
Public ReadOnly Property ProcessorCount As Long
Get
Return _processorCount
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal totalMemory As Long, ByVal processorCount As Int32)
Me._totalMemory = totalMemory
Me._processorCount = processorCount
End Sub
Public Overrides Function Validate(context As ILicenseContext, type As Type, instance As Object) As ValidationResult
Dim sys = DirectCast(context.GetService(GetType(ISystemInformation)), ISystemInformation)
Dim totalMem = ToMegabytes(sys.TotalPhysicalMemory)
If (totalMem < Me.TotalMemory Or sys.ProcessorCount < Me.ProcessorCount) Then
' Cannot run on this machine
Return ValidationResult.NotPassed
End If
Return MyBase.Validate(context, type, instance)
End Function
Private Shared Function ToMegabytes(ByVal value As Long) As Long
Return value / 1024 / 1024
End Function
Public Function GetSchema() As Xml.Schema.XmlSchema Implements IXmlSerializable.GetSchema
Return Nothing
End Function
Public Sub ReadXml(reader As Xml.XmlReader) Implements IXmlSerializable.ReadXml
reader.MoveToContent()
Dim totalMemory = reader.GetAttribute("totalMemory")
If Not String.IsNullOrEmpty(totalMemory) Then
Me._totalMemory = XmlConvert.ToInt64(totalMemory)
End If
Dim processorCount = reader.GetAttribute("processorCount")
If Not String.IsNullOrEmpty(processorCount) Then
Me._processorCount = XmlConvert.ToInt32(processorCount)
End If
Dim isEmptyElement = reader.IsEmptyElement
reader.ReadStartElement()
If Not isEmptyElement Then
reader.ReadEndElement()
End If
End Sub
Public Sub WriteXml(writer As Xml.XmlWriter) Implements IXmlSerializable.WriteXml
If Me.TotalMemory > 0 Then
writer.WriteAttributeString("totalMemory", XmlConvert.ToString(TotalMemory))
End If
If Me.ProcessorCount > 0 Then
writer.WriteAttributeString("processorCount", XmlConvert.ToString(ProcessorCount))
End If
End Sub
End Class
CreateCustomSignatureProvider
Event queue for all listeners interested in CreateCustomSignatureProvider events.
public event EventHandler<CreateCustomSignatureProviderEventArgs> CreateCustomSignatureProvider