Skip to content

Instantly share code, notes, and snippets.

@markadrake
Last active December 30, 2025 18:50
Show Gist options
  • Select an option

  • Save markadrake/f6c00123ad177e7d4395849c079ab0e9 to your computer and use it in GitHub Desktop.

Select an option

Save markadrake/f6c00123ad177e7d4395849c079ab0e9 to your computer and use it in GitHub Desktop.
Force Users to Enable Two-Factor Authentication in Umbraco 13
(() => {
angular.module("umbraco").run(["authResource", "twoFactorLoginResource", "editorService", "notificationsService", "eventsService", async (authResource, twoFactorLoginResource, editorService, notificationsService, eventsService) => {
const state = {};
const init = async (firstInit) => {
"app.ready"
state.currentUser = await authResource.getCurrentUser();
state.twoFactorAuthProviders = await twoFactorLoginResource.get2FAProvidersForUser(state.currentUser.id);
if(firstInit && !twoFactorIsEnabled()) {
configureTwoFactor();
}
};
const twoFactorIsEnabled = () => {
return !!state.twoFactorAuthProviders.find(provider => provider.isEnabledOnUser);
};
const configureTwoFactor = () => {
editorService.open({
create: true,
user: state.currentUser,
isCurrentUser: true,
size: "small",
view: "views/common/infiniteeditors/twofactor/configuretwofactor.html",
close: async () => {
await init();
if(twoFactorIsEnabled()) {
notificationsService.success("Two-Factor Auth", "You have successfully configured two-factor authentication.");
editorService.close();
} else {
notificationsService.error("Two-Factor Auth", "You must configure two-factor authentication before continuing.");
}
}
});
};
eventsService.on("app.ready", (name, args) => {
init(true);
});
}]);
})();
{
"javascript": [
"~/App_Plugins/path/to/EnforceTwoFactorAuthentication.js"
]
}
@markadrake
Copy link
Author

markadrake commented Feb 25, 2024

Suggestions:

  1. Add a setting to appsettings.json to enforce this policy.
  2. Add localization to support multiple languages.
  3. Consider extending the backend with the same checks—this is a UI solution only.

@InfiniteSpirals
Copy link

Hey this is great! Just thought I'd ask on the off-chance but have you managed to port this over to Lit / Belissima for U14/15 yet?

@markadrake
Copy link
Author

@InfiniteSpirals Good question! I'll try to tackle something similar over the weekend against the latest version of 15.

@InfiniteSpirals
Copy link

@markadrake - fantastic! :)

@ramlakhan78
Copy link

ramlakhan78 commented Apr 29, 2025

Hey @markadrake
This works in umbraco 13 but not for 14/15.
Can you please let me know, have you tried same for umbraco version 15?

@richard-arkom
Copy link

Where abouts in Umbraco 13 would you place this code, and is there any other config to do other than the usual Umbraco 2FA stuff?

@SSPLGautam
Copy link

I've implemented 2fa encforcement in v17 🎉
Here is the gist document: https://gist.github.com/SSPLGautam/f2f9fafea5240e199ddf6f2ff938ae34
I've left a todo here for the notification service because the notification gets opened behind the mfa modal.

@markadrake
Copy link
Author

Hey @markadrake This works in umbraco 13 but not for 14/15. Can you please let me know, have you tried same for umbraco version 15?

@ramlakhan78 did you see the solution above? Quite sorry I have not had time to work on this further. Would love to know what you think or @SSPLGautam's solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment