Create Custom Alert Event Receivers

Alert Event Receivers are class libraries you develop and publish in Workflow Studio to respond to one or more Alert Events. Each one is capable of listening for and responding to any number of subscribed events that occur in a given environment. One Alert Event Receiver is capable of performing more than one task per event and each event can be subscribed to by more than one Alert Event Receiver. You can create custom Alert Event Receivers to perform any desired action when a corresponding alert to which it is subscribed occurs. You create custom Alert Event Receivers by deriving them from the base AlertEventReceiver abstract class, overriding the OnProcessAlert() method with your specific implementation of the abstract class. 

In this topic, we demonstrate how to create a custom Alert Event Receiver that creates a log event and sends an email notification whenever users become locked out of their accounts. 

To create a custom Alert Event Receiver for locked out users

  1. In Workflow Studio, click the application icon to open the application menu and select Extensibility, then EmpowerID Alert Event Receiver.



  2. Name the event receiver and select the package under which to create it.



    This opens the C# Editor for the custom Alert Event Receiver. Notice that the class automatically derives from the base AlertEventReceiver class.



  3. From the OnProcessAlert() method, replace the TODO comment with code that does the following: 
    • Creates a new Person object from the locked-out user
    • Calls a custom method, the AddAuditLog() method to create a new audit log entry, passing in the locked-out user as the parameter
    • Calls a second custom method, the SendLockedOutEmail() method to send out email notifications, passing in the locked-out user as the parameter

      The method looks like this:

      protected override void OnProcessAlert(AlertExecutionContext context)
      {
         Person person = (Person)context.Current.Properties["Person"];
         AddAuditLog(person);
         SendLockedOutEmail(person);			 
      }
  4. Below the OnProcessAlert() method, add code to create the method stub for the AddAuditLog() method and then in the method body add code to do the following:
    • Create a new instance of the AuditLogOperation class, named "log." This class has methods and properties that allow you to create operation logs.
    • Set some properties on log and call the Insert() method to add the log event to the EmpowerID Identity Warehouse
    • Write a message to DebugView if the log instance is not inserted

      The method looks like this:

      private void AddAuditLog(Person person)
      {
         AuditLogOperation log = new AuditLogOperation();
         log.Name = "Person account locked out";
         log.FriendlyName = "Person account for " + person.FriendlyName + " has been locked out due to too many failed login attempts.";
         log.AuditLogOperationTypeID = 8;
         log.TargetResourceID = person.ResourceID;
         log.ActorPersonID = person.PersonID;
         bool success = log.Insert();
         if (!success) TheDotNetFactory.Framework.WorkflowCoreServices.Debug.WriteLine("******Insert Failed!*********");
      }
  5. Below the AddAuditLog() method, add code to create the method stub for the SendLockedOutEmail() method and then in the method body add code to do the following:
    • Create a new dictionary
    • Add "WorkflowMessage1" to the dictionary and set it to the FriendlyName property of the person who is locked out of EmpowerID

      WorkflowMessage1 is one of the EmpowerID custom email wildcards that can be used to create a custom message.

    • Create a new list for holding Person objects
    • Call the SendEmailAsynch() method of the AlertEmailUtility class to send an email to the locked-out person notifying them that they have been locked out of EmpowerID

      The method looks like this:

      private void SendLockedOutEmail(Person person)
      {
         Dictionary<string,object> properties = new Dictionary<string,object>();
         properties["WorkflowMessage1"] = person.FriendlyName;
         var peopleToEmail = new E.TList<C.Person>();
         peopleToEmail.Add(person);			
         TheDotNetFactory.Framework.AlertEmailUtility.SendEmailAsynch(properties, null, null, 1029, null, null, null, "English", peopleToEmail);
      }


      The complete code for the event receiver looks like this:

      public class PersonLockedOutFromAccount : AlertEventReceiver
      {
         protected override void OnProcessAlert(AlertExecutionContext context)
         {
      	Person person = (Person)context.Current.Properties["Person"];
      	AddAuditLog(person);
      	SendLockedOutEmail(person);			 
         }
      		
         private void AddAuditLog(Person person)
         {
      	AuditLogOperation log = new AuditLogOperation();
      	log.Name = "Person account locked out";
              log.FriendlyName = "Person account for " + person.FriendlyName + " has been locked out due to too many failed login attempts.";
              log.AuditLogOperationTypeID = 8;
              log.TargetResourceID = person.ResourceID;
              log.ActorPersonID = person.PersonID;
              bool success = log.Insert();
      	if (!success) TheDotNetFactory.Framework.WorkflowCoreServices.Debug.WriteLine("******Insert Failed!*********");
         }
      
         private void SendLockedOutEmail(Person person)
         {
      	Dictionary<string,object> properties = new Dictionary<string,object>();
      	properties["WorkflowMessage1"] = person.FriendlyName;
      	E.TList<C.Person> peopleToEmail = new E.TList<C.Person>();
      	peopleToEmail.Add(person);			
      	TheDotNetFactory.Framework.AlertEmailUtility.SendEmailAsynch(properties, null, null, 1029, null, null, null, "English", peopleToEmail);
         }
      }
  6. In the Properties grid, set the Alerts property to PersonLockedOut.