Developer forum

Forum » Integration » Notification for Login blocked

Notification for Login blocked

Nuno Aguiar
Reply

Hi,

 

Is there a notification that get's triggered when a login is locked/blocked after X failed attempts?

 

We have a project where we need to actually deactivate the user when that happens.

 

Best Regards,

Nuno Aguiar


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Hi Nuno,

You can look at LoginFailed and then look at the Reason property. Here's an example that sends an email when this happens:

using System;
using System.Net.Mail;
using Dynamicweb;
using Dynamicweb.Extensibility;
using Dynamicweb.Frontend;
using Dynamicweb.Notifications;
using Dynamicweb.Rendering;
using NLog;

namespace Your.Namespace
{
  [Subscribe(Standard.User.OnExtranetLoginFailed)]
  public class SendMailWhenUserIsLockedOut : NotificationSubscriber
  {
    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

    private const string TemplatePath = "UserManagement/LockedUsers/LockedOutUserEmailTemplate.cshtml";
    private const string FromAddress = "info@goodsource.com";
    private const string ToAddress = "imar.spaanjaars@gmail.com";
    private const string Subject = "GoodSource E-Commerce account disabled notification";

    public override void OnNotify(string notification, NotificationArgs args)
    {
      Logger.Log(LogLevel.Debug, string.Format("Firing {0}", notification));
      var localArgs = args as Standard.User.OnExtranetLoginFailedArgs;
      if (localArgs == null)
      {
        return;
      }
      if (localArgs.Reason == Standard.User.OnExtranetLoginFailedArgs.FailReason.LoginLocked)
      {
        Logger.Log(LogLevel.Debug, "Firing for LoginLocked");
        string toAddress;
        try
        {
          var pageView = PageView.Current();
          pageView.Redirect = false;
          toAddress = pageView.Area.Item["EmailAddressLockedUsers"].ToString();
          Logger.Log(LogLevel.Debug, string.Format("Found addresses: {0}", toAddress));
        }
        catch (Exception)
        {
          toAddress = ToAddress;
        }
        if (string.IsNullOrEmpty(toAddress))
        {
          toAddress = ToAddress;
        }
        var template = new Template(TemplatePath);
        template.SetTag("UserName", localArgs.Username);

        var user = Dynamicweb.Modules.UserManagement.User.GetUserByUserName(localArgs.Username);

        if (user != null && user.CustomFieldValues != null)
        {
          Logger.Log(LogLevel.Debug, string.Format("Found user: {0}", user.UserName));
          template.SetTag("User:Name", user.Name);
          template.SetTag("User:FirstName", user.FirstName);
          template.SetTag("User:LastName", user.LastName);
          template.SetTag("User:MiddleName", user.MiddleName);
          template.SetTag("User:Email", user.Email);

          foreach (var field in user.CustomFieldValues)
          {
            template.SetTag("User:" + field.CustomField.SystemName, field.Value.ToString());
          }
          using (var message = new MailMessage(FromAddress, toAddress, Subject, template.Output()))
          {
            message.IsBodyHtml = true;
            EmailHandler.Send(message);
          }
        }
        else
        {
          Logger.Log(LogLevel.Debug, string.Format("Didn't find user: {0}", localArgs.Username));
        }
      }
    }
  }
}

 

Votes for this answer: 1
 
Nuno Aguiar
Reply

Hi Imar,

 

Perfect, just what I was looking for. Thanks.

 

Nuno Aguiar

 

You must be logged in to post in the forum