Created
October 4, 2015 20:55
-
-
Save PNergard/595e36a7b6ae55dd579b to your computer and use it in GitHub Desktop.
A scheduled job that monitors the license expiry date for EPiServer CMS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected],[email protected]"; | |
private const string SenderAddress = "[email protected]"; | |
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