Skip to content

IAuthenticationHook

An authentication hook runs inside the Login form component when a website user signs in. It can stop a sign-in before the password is checked, and after the attempt it can send the user to another page. Implement IAuthenticationHook to add your own check, such as a maintenance lock or a required setup step.

Interface declaration

	namespace Veva.Core.Application.BlazorComponents;

	public interface IAuthenticationHook
	{
		AuthenticationValidationResult ValidateLogin(string username, string password, AuthenticationHookContext context);

		Task<AfterLoginResult> AfterLogin(bool loginSuccessful, string username, string password, AuthenticationHookContext context);
	}

Example

This hook lets an editor close sign-in while the site is being maintained. While Locked is on, the Login form shows the editor's message and does not check the password.

	using System.ComponentModel;
	using Veva.Core.Application.BlazorComponents;

	[DisplayName("Maintenance lock")]
	[Description("Stops sign-ins while the site is being maintained")]
	public class MaintenanceLockHook : IAuthenticationHook
	{
		[EditorBrowsable]
		[DisplayName("Locked")]
		[Description("While this is on, nobody can sign in")]
		public bool Locked { get; set; }

		[EditorBrowsable]
		[DisplayName("Message")]
		[Description("Shown on the form while sign-in is locked")]
		public string Message { get; set; } = "Sign-in is closed for maintenance. Please try again later.";

		public AuthenticationValidationResult ValidateLogin(string username, string password, AuthenticationHookContext context)
		{
			var result = new AuthenticationValidationResult { AuthenticationSucceeded = !Locked };

			if (Locked)
			{
				// The Login form lists Messages when the check fails: never leave it empty.
				result.AddMessage(Message);
			}

			return result;
		}

		public Task<AfterLoginResult> AfterLogin(bool loginSuccessful, string username, string password, AuthenticationHookContext context)
		{
			// None lets the Login form carry on as usual.
			return Task.FromResult(new AfterLoginResult { Result = AfterLoginResultType.None });
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Application.BlazorComponents;
	using Veva.Core.Application.DependencyInjection;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// The key is the class name; the Login form stores it when an editor picks the hook.
		services.AddNamed<IAuthenticationHook, MaintenanceLockHook>(ServiceLifetime.Transient);
	}

Turning it on. Registering a hook adds it to the Login form component's Authentication hooks setting, under Advanced. An editor picks a hook there and fills in its settings. The list shows the class's [DisplayName] and [Description], and public read/write properties marked [EditorBrowsable] become the hook's settings. The form stores the hook under its registration name, which is the class name, so renaming the class drops it from forms that use it. The setting holds one hook: to run two checks, put them in one class.

How it runs. When the user submits the form, the Login form first calls ValidateLogin. If the result has AuthenticationSucceeded false, the form shows each string in Messages as an error and stops: the password is never checked. Otherwise the form checks its own fields and tries to sign the user in. Then it calls AfterLogin with the outcome, whether the sign-in succeeded or not. If AfterLogin returns AfterLoginResultType.Redirect with a RedirectUrl, the browser goes there and the form's own redirect is skipped. With AfterLoginResultType.None the form carries on as usual: to the return URL or the chosen page after a success, or to an error message or the two-factor step after a failure.

Two-factor sign-in. When the user has two-factor authentication on, the password step does not finish the sign-in, so AfterLogin gets loginSuccessful false. The hook is not called again after the verification code. A hook that redirects after a failed sign-in therefore also sends two-factor users away before they can enter their code.

What you can read. context.PageRequest gives the page, the website and the visitor, context.DataBindingContext the data binding values on the page, and context.FormContext the form itself with the values of its fields. username is what the user typed in the UserName field.

Built-in hooks. Veva ships Prevent authentication, which blocks all sign-ins with a message, and Enforce MFA, which sends a user without two-factor authentication to a setup page. The Passkeys module adds Passkey enrollment, which sends a user without a passkey to an enrollment page. The last two redirect from AfterLogin and get the page's address from IPageUrlService.

Other sign-in components. The Phone login and SmartID login components in the Veva.Modules.Signet package also have an Authentication hooks setting. They pass the phone number or the national ID as username and an empty password.

Pitfalls. ValidateLogin is not async, so keep it quick. The password reaches your code in plain text: never log or store it. Register the hook as Transient. The editor's settings are written onto the instance, and the default scoped lifetime can hand the same instance to two forms with different settings. The hook is created from the service container, so constructor injection works.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva