تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
مقال: DotNET Components Licencing
#1
السلام عليكم ورحمة الله
عادة عندما ترغب في توزيع مكتباتك الخاصة على مطوِّرين آخرين فإنك ترغب في حماية أدواتك من الوصول الغير مرغوب فيه, كالوصول إليها من قبل المطوِّرين المجهولين الذين لم تقم بترخيص منتجاتك لهم.
بالرغم من استخدامها النادر في تطبيقات .NET Framework إلا أن مجال الأسماء System.ComponentModel يقدِّم فئة موفِّر التراخيص LicenseProvider Class. والتي هي محور النقاش.
الخطوات الضرورية لترخيص منتجاتك هي:
إنشاء الترخيص, والذي سيتم تضمينه مع مكتباتك الخاصة عندما تقوم بنشرها باستخدام ناشر موثوق به.
إنشاء موفِّر الترخيص License Provider, يجب إنشاء فئة موروثة عن الفئة LicencseProvider داخل مكتباتك, وكتابة أساليب التحقق من الترخيص.
التعليمات البرمجية التالية تقدم مثالا عن كيفية توريث الفئة Licencse:

كود C#

كود :
public class MyCustomLicense : License
{
public override void Dispose()
{
}

public override string LicenseKey
{
get
{
return Guid.NewGuid().ToString();
}
}
}
كود VB

كود :
Public Class MyCustomLicense
Inherits License
Public Overloads Overrides Sub Dispose()
End Sub

Public Overloads Overrides ReadOnly Property LicenseKey() As String
Get
Return Guid.NewGuid().ToString()
End Get
End Property
End Class
الخطوة التالية هي إنشاء فئة موروثة عن الفئة LicenseProvider, وتنفيذ الأساليب المناسبة:

كود C#

كود :
public class MyCustomLicenseProvider : LicenseProvider
{
public override License GetLicense(
LicenseContext context, Type type,
object instance, bool allowExceptions)
{
// Do some logic to go figure out if this type or instance
// is licensed. This can be implemented however you want.
bool licenseIsValid;


// If license check isn't successful:
if (licenseIsValid)
{
throw new LicenseException(type, instance, "Invalid license.");
}
else
{
return new MyCustomLicense();
}
}
}
كود VB

كود :
Public Class MyCustomLicenseProvider
Inherits LicenseProvider
Public Overloads Overrides Function GetLicense(ByVal context As LicenseContext, ByVal type As Type, ByVal instance As Object, ByVal allowExceptions As Boolean) As License
' Do some logic to go figure out if this type or instance
' is licensed. This can be implemented however you want.
Dim licenseIsValid As Boolean


' If license check isn't successful:
If licenseIsValid Then
Throw New LicenseException(type, instance, "Invalid license.")
Else
Return New MyCustomLicense()
End If
End Function
End Class
كيف يتم عملياً ترخيص المنتج لاستخدام؟
لتنفيذ ذلك قم بإضافة السمة LicenseProvider Attribute إلى الفئة الأساسية في مشروعك.

كود C#

كود :
[LicenseProvider(typeof(MyCustomLicenseProvider))]
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
// Attempt to validate the license
LicenseManager.Validate(typeof(Program));

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (Exception exception)
{
MessageBox.Show("An unhandled application exception occurred." + Environment.NewLine + Environment.NewLine + exception.ToString());
}
}
}
كود VB

كود :
<LicenseProvider(GetType(MyCustomLicenseProvider))> _
Module Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread()> _
Private Sub Main()
Try
' Attempt to validate the license
LicenseManager.Validate(GetType(Program))

Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
Catch exception As Exception
MessageBox.Show(("An unhandled application exception occurred." & Environment.NewLine) + Environment.NewLine + exception.ToString())
End Try
End Sub
End Module
عند استدعاء الأسلوب LicenseManager.Validate() يقوم CLR بإنشاء Thread فرعي يقوم هو الآخر باستدعاء الإجراء GetLicence()
الغامض في الأمر هو كيفية يقوم الأسلوب GetLicense من التحقق من الترخيص.
سيقوم الأسلوب GetLicense من التحقق من وجود ملف الترخيص, ويقوم بتحليل ملف الترخيص بطريقة ما. يمكنك التحقق من الترخيص بأسلوبك الخاص.
على سبيل المثال يمكنك الاستعانة بالأسلوب التالي كوسيلة للتحقق من الترخيص:

كود C#

كود :
public override License GetLicense(LicenseContext context, Type type,
object instance, bool allowExceptions)
{
// Get the path of the would-be license file
string path = Path.GetFullPath(".");
string licenseFile = Path.Combine(path, "MyCustomLicense.lic");

// See if the file exists, if it doesn't - exception out
if (!File.Exists(licenseFile))
{
throw new LicenseException(type, instance,
"A licensing error occurred. The MyCustomLicense licensing file was not found in the following location: '"
+ licenseFile + "'. Please place the license file in this location and try the application again.");
}

// Read the contents of the file, if it's null or empty - exception out
string fileContents = File.ReadAllText(licenseFile);
if ( string.IsNullOrEmpty(fileContents))
{
throw new LicenseException(type, instance,
"A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '"
+ licenseFile + "'. However, the file is empty. Please place the license file in this location and try the application again.");
}

// Deserialize this XML file into a real data structure. If there was a problem deserializing - exception out
XmlDomainObjects.License license = null;
try
{
license =
LicenseUtilities.Deserialize<XmlDomainObjects.License>(fileContents, "license");
}
catch (LicenseUtilityException exception)
{
throw new LicenseException(type, instance,
"A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '"
+ licenseFile + "'. However, the file does not appear to be a valid license file. Please place the license file in this location and try the application again.");
}

// Generate a hash from the domain properties
string hash = LicenseUtilities.GetHashForLicense(license);

// See if the generated hash matches what was in the file, if not - exception out
if (!license.Hash.Equals(hash, StringComparison.InvariantCulture))
{
throw new LicenseException(type, instance,
"A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '"
+ licenseFile + "'. However, it has been modified and can no longer be verified. Please replace this file with the correct license file or contact support for a new license file.");
}

return license;
}
كود VB

كود :
Public Overloads Overrides Function GetLicense(ByVal context As LicenseContext, ByVal type As Type, ByVal instance As Object, ByVal allowExceptions As Boolean) As License
' Get the path of the would-be license file
Dim path__1 As String = Path.GetFullPath(".")
Dim licenseFile As String = Path.Combine(path__1, "MyCustomLicense.lic")

' See if the file exists, if it doesn't - exception out
If Not File.Exists(licenseFile) Then
Throw New LicenseException(type, instance, "A licensing error occurred. The MyCustomLicense licensing file was not found in the following location: '" & licenseFile & "'. Please place the license file in this location and try the application again.")
End If

' Read the contents of the file, if it's null or empty - exception out
Dim fileContents As String = File.ReadAllText(licenseFile)
If String.IsNullOrEmpty(fileContents) Then
Throw New LicenseException(type, instance, "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" & licenseFile & "'. However, the file is empty. Please place the license file in this location and try the application again.")
End If

' Deserialize this XML file into a real data structure. If there was a problem deserializing - exception out
Dim license As XmlDomainObjects.License = Nothing
Try
license = LicenseUtilities.Deserialize(Of XmlDomainObjects.License)(fileContents, "license")
Catch exception As LicenseUtilityException
Throw New LicenseException(type, instance, "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" & licenseFile & "'. However, the file does not appear to be a valid license file. Please place the license file in this location and try the application again.")
End Try

' Generate a hash from the domain properties
Dim hash As String = LicenseUtilities.GetHashForLicense(license)

' See if the generated hash matches what was in the file, if not - exception out
If Not license.Hash.Equals(hash, StringComparison.InvariantCulture) Then
Throw New LicenseException(type, instance, "A licensing error occurred. The MyCustomLicense licensing file was found in the following location: '" & licenseFile & "'. However, it has been modified and can no longer be verified. Please replace this file with the correct license file or contact support for a new license file.")
End If

Return license
End Function
يتضح من الكود السابق ملاحظتين هامتين, هما استخدام تقنيتي Hashing و Serializing, إنها فقط مجرد أساليب مساعدة للتحقق, لكن لا يمكن الاعتماد بأي حال على هذه الأساليب لترخيص البرامج التجارية لأنها مكشوفة. لأنه بهذا الأسلوب يمكن لأي أحد الحصول على ترخيص باستخدام مكتباتك.
نقطة حرجة أخرى وهي إمكانية ابتلاع الاستثناء LicenseException
إذا كنت ترغب حقاً في استخدام ترخيص منتج للبيع عندها يجب عليك استخدام تقنية Public-Key من أجل "تنشيط المنتج" على سبيل المثال.
}}}
تم الشكر بواسطة:



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم