Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A scheduled job that monitors the license expiry date for EPiServer CMS.
using System;
using System.Linq;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.Licensing;
using System.Security.Cryptography;
using EPiServer.Framework.Configuration;
using EPiServer.Licensing.RestrictionTypes;
using System.Net.Mail;
namespace LicenseExperyScheduledJob
{
[ScheduledPlugIn(DisplayName = "LicensExpiryScheduledJob")]
public class LicensExpiryScheduledJob : ScheduledJobBase
{
#region Constants
private const int MonthValue = 1;
private const string ToAdresses = "per.nergard@knowit.se,per.nergard@knowit.se";
private const string SenderAddress = "no-reply@nodomain.se";
private const string MailSubject = "The EPiServer license is about to expire.";
private const string MailHtml = @"<html><body><div>{0}</div></body></html>";
private const string MailMesage = "The EPiServer license expires: <b>{0}</b>";
private const string OkMessage = "No warning sent.";
private const string NotOkMessage = "Warning sent. License expires {0}, date now {1}";
private const string NoLicenseData = "Job failed. No license data was found";
#endregion
private bool _stopSignaled;
public LicensExpiryScheduledJob()
{
IsStoppable = true;
}
/// <summary>
/// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
/// </summary>
public override void Stop()
{
_stopSignaled = true;
}
/// <summary>
/// Called when a scheduled job executes
/// </summary>
/// <returns>A status message to be stored in the database log and visible from admin mode</returns>
public override string Execute()
{
//Call OnStatusChanged to periodically notify progress of job for manually started jobs
OnStatusChanged(String.Format("Starting execution of {0}", this.GetType()));
var data = GetLicenseInformation("episerver");
if (data == null) return NoLicenseData;
Restriction restriction = data.Restrictions[2];
RestrictionEntry restictionentry = restriction.Entries[0];
var licenseRestrictionDate = ((ValidToRestriction)restictionentry).Date;
if (DateTime.Now > licenseRestrictionDate.AddMonths(-MonthValue))
{
SendMail(licenseRestrictionDate);
return string.Format(NotOkMessage, licenseRestrictionDate, DateTime.Now);
}
else
return OkMessage;
}
#region Methods
/// <summary>
/// Gets the specified licensdata object
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private LicenseData GetLicenseInformation(string name)
{
var publickey = "<KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>l8slyw1cbtPccvCM/sgrErnNXJMMYwxsuV74so72eENdDwyV1g4Z1WvVre2Dn6q02bSDlneNhV/625RRD4EG1L9cu8WFWvE/6vCsHy/zA+loVaXnvsJiBmK4j7JDTwRtvE51aoBpiJOkNfrSh3HVD12uxsHW0dMXpMgNSlyoEns=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo>";
LicenseDataCollection licenseData = null;
RSA key = RSA.Create();
key.FromXmlString(publickey);
try
{
licenseData = LicenseData.Load(EPiServerFrameworkSection.Instance.Licensing.LicenseFilePath, null, key);
}
catch (Exception exception)
{
}
return licenseData.FirstOrDefault(x => String.Equals(x.Name, name, StringComparison.InvariantCultureIgnoreCase));
}
private void SendMail(DateTime expiryDate)
{
var msg = new MailMessage();
msg.From = new MailAddress(SenderAddress);
msg.Subject = MailSubject;
msg.To.Add(ToAdresses);
msg.IsBodyHtml = true;
msg.Body = string.Format(MailHtml, string.Format(MailMesage, expiryDate.ToString()));
SmtpClient client = new SmtpClient();
client.Send(msg);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.