---
title: Handling custom multi-factor authentication
related:
  - https://docs.kentico.com/k81/custom-development/handling-global-events.md
  - https://docs.kentico.com/k81/managing-users/user-registration-and-authentication/configuring-multi-factor-authentication.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

You can use [global events](https://docs.kentico.com/k81/custom-development/handling-global-events.md) to define custom actions that the system performs after a user tries to sign in to Kentico using the username and password and the [multi-factor authentication](https://docs.kentico.com/k81/managing-users/user-registration-and-authentication/configuring-multi-factor-authentication.md) is enabled. See the **SecurityEvents** section of the [global event reference](https://docs.kentico.com/k81/custom-development/handling-global-events/reference-global-system-events.md#securityevents) to learn more about the available options.

> **Info:** If you want to implement your own authentication factor, clear the **Display initialization token** option in **Settings** **-> Security & Membership -> Authentication**.

To set up a custom action, which is performed after a user tries to sign in to Kentico, implement a handler for the **SecurityEvents.MultiFactorAuthenticate.Execute** event. For example, you can implement functionality that sends users an SMS text with a passcode.

This is a simple example of writing information into the event log. The example uses a custom class in the App\_Code folder:

```csharp

using System.Data;

using CMS.Base;
using CMS.Membership;
using CMS.EventLog;

[CustomMFAAuthentication]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    public class CustomMFAAuthenticationAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            // Assigns a handler to the SecurityEvents.MultiFactorAuthenticate.Execute event
            // This event occurs when users try to sign in to Kentico with multi-factor authentication enabled
            SecurityEvents.MultiFactorAuthenticate.Execute += MFAuthentication_Execute;
        }

        // The handler method, which writes the passcode to the event log
        // You can replace it with your custom code
        private void MFAuthentication_Execute(object sender, AuthenticationEventArgs e)
        {
            string message = string.Format("Passcode: {0}", e.Passcode);
            EventLogProvider.LogInformation("Multi-factor authentication", "Passcode", message);
         }
    }
}


```

**Note:** The Passcode parameter in its default state is 30 characters long, but the multi-factor authentication in Kentico uses only the **first 5 characters**.
