📄 ftblicenseprovider.cs
字号:
namespace FreeTextBoxControls.Licensing
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
public class FtbLicenseProvider : LicenseProvider
{
static FtbLicenseProvider()
{
FtbLicenseProvider.encryptionKeyBytes = new byte[] { 0x39, 0x48, 0x42, 50, 0x38, 0x31, 70, 0x36 };
FtbLicenseProvider.LicenseCollector = new FtbLicenseCollector();
}
protected virtual FtbLicense CreateEmptyLicense(Type type)
{
return new FtbLicense(type, string.Empty, string.Empty);
}
protected virtual FtbLicense CreateLicense(Type type, string licenseData)
{
Match match1 = Regex.Match(licenseData, type.Name + " License\r\n\\[(?<licenseType>[^\\]]+)\\]\r\n\\[(?<secondField>[^\\]]+)\\]");
if (match1.Success)
{
string text1 = this.DecryptLicenseData(match1.Groups["licenseType"].Value);
string text2 = this.DecryptLicenseData(match1.Groups["secondField"].Value);
if ((text1 == "SingleLicense") || (text1 == "DistributionLicense"))
{
return new FtbLicense(type, text1, text2, true);
}
if (text1 == "ExpiringLicense")
{
DateTime time1 = new DateTime();
try
{
time1 = Convert.ToDateTime(text2);
}
catch
{
}
if ((time1 != new DateTime()) && (time1 > DateTime.Now))
{
return new FtbLicense(type, text1, text2, true);
}
}
else
{
return new FtbLicense(type, "BadLicenseData", text1 + "," + text2, false);
}
}
if (HttpContext.Current.Request.Url.AbsoluteUri.StartsWith("http://localhost/"))
{
return new FtbLicense(type, "LocalhostLicense", "none", true);
}
return new FtbLicense(type, "NoLicense", "Unlicensed", false);
}
private string DecryptLicenseData(string input)
{
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
ICryptoTransform transform1 = provider1.CreateDecryptor(FtbLicenseProvider.encryptionKeyBytes, FtbLicenseProvider.encryptionKeyBytes);
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
byte[] buffer1 = new byte[input.Length];
try
{
buffer1 = Convert.FromBase64CharArray(input.ToCharArray(), 0, input.Length);
}
catch (Exception)
{
return "Error. Input Data is not base64 encoded.";
}
long num1 = 0;
long num2 = input.Length;
try
{
while (num2 >= num1)
{
stream2.Write(buffer1, 0, buffer1.Length);
num1 = stream1.Length + Convert.ToUInt32((int) ((buffer1.Length / provider1.BlockSize) * provider1.BlockSize));
}
string text1 = new ASCIIEncoding().GetString(stream1.GetBuffer(), 0, (int) stream1.Length);
string text2 = text1.Substring(0, 5);
int num3 = Convert.ToInt32(text2);
return text1.Substring(5, num3);
}
catch (Exception)
{
return "";
}
}
public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
{
FtbLicense license1 = null;
string text1 = null;
if (context.UsageMode == LicenseUsageMode.Designtime)
{
return this.CreateEmptyLicense(type);
}
license1 = FtbLicenseProvider.LicenseCollector.GetLicense(type);
if (license1 == null)
{
string text2 = this.GetLicenseData(type);
if ((text2 != null) && (text2.Length != 0))
{
if (this.ValidateLicenseData(type, text2))
{
FtbLicense license2 = this.CreateLicense(type, text2);
if (this.ValidateLicense(license2, out text1))
{
license1 = license2;
FtbLicenseProvider.LicenseCollector.AddLicense(type, license1);
}
}
}
else
{
license1 = this.CreateLicense(type, "");
}
}
else if (!this.ValidateLicense(license1, out text1))
{
license1 = null;
}
return license1;
}
protected virtual string GetLicenseData(Type type)
{
string text1 = null;
Stream stream1 = null;
try
{
stream1 = this.GetLicenseDataStream(type);
if (stream1 != null)
{
StreamReader reader1 = new StreamReader(stream1);
text1 = reader1.ReadToEnd();
reader1.Close();
}
}
finally
{
if (stream1 != null)
{
stream1.Close();
stream1 = null;
}
}
return text1;
}
protected virtual Stream GetLicenseDataStream(Type type)
{
string text1 = type.Assembly.GetName().Name;
string text2 = type.Assembly.GetName().Version.ToString();
string text3 = "~/bin/" + type.Name + ".lic";
string text4 = null;
try
{
text4 = HttpContext.Current.Server.MapPath(text3);
if (!File.Exists(text4))
{
text3 = "/bin/" + type.Name + ".lic";
text4 = HttpContext.Current.Server.MapPath(text3);
if (!File.Exists(text4))
{
text4 = null;
}
}
}
catch
{
}
if (text4 != null)
{
try
{
Stream stream1 = new FileStream(text4, FileMode.Open, FileAccess.Read, FileShare.Read);
if (stream1 == null)
{
return null;
}
return stream1;
}
catch
{
return null;
}
}
return null;
}
protected virtual bool ValidateLicense(FtbLicense license, out string errorMessage)
{
errorMessage = null;
return true;
}
protected virtual bool ValidateLicenseData(Type type, string licenseData)
{
Match match1 = Regex.Match(licenseData, type.Name + " License\r\n\\[(?<type>[^\\]]+)\\]\r\n\\[(?<secondfield>[^\\]]+)\\]");
return match1.Success;
}
private static readonly byte[] encryptionKeyBytes;
private static readonly FtbLicenseCollector LicenseCollector;
private sealed class FtbLicenseCollector
{
public FtbLicenseCollector()
{
this._collectedLicenses = new HybridDictionary();
}
public void AddLicense(Type objectType, FtbLicense license)
{
if (objectType == null)
{
throw new ArgumentNullException("objectType");
}
if (license == null)
{
throw new ArgumentNullException("objectType");
}
this._collectedLicenses[objectType] = license;
}
public FtbLicense GetLicense(Type objectType)
{
if (objectType == null)
{
throw new ArgumentNullException("objectType");
}
if (this._collectedLicenses.Count == 0)
{
return null;
}
return (FtbLicense) this._collectedLicenses[objectType];
}
public void RemoveLicense(Type objectType)
{
if (objectType == null)
{
throw new ArgumentNullException("objectType");
}
this._collectedLicenses.Remove(objectType);
}
private IDictionary _collectedLicenses;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -