Kentico CMS登录成功后更新用户字段需绑定哪个事件咨询
Great question! For your scenario where you need to update user fields (like disabling UserMFRequired and updating last logon time) after a successful login with your custom MFA authentication module, the AuthenticationSuccess event is the perfect fit.
Why AuthenticationSuccess?
This event fires after the user has successfully completed all authentication steps—including your custom MFA verification. That means you can be 100% sure the user is fully authenticated before making any changes to their profile, avoiding race conditions or premature updates that could break your MFA flow.
How to Implement It
Here's a practical, Kentico-compliant example tailored to your custom MFAuthentication module:
Register the event in your module initialization
Hook into the event when your custom MFA module starts up. This ensures the handler is active whenever the CMS runs.Update user fields in the event handler
Grab the authenticated user, modify the required fields, and save changes using Kentico's built-in user management APIs to maintain data integrity.
using CMS; using CMS.Membership; using CMS.Base; using System; // Register your custom MFA module with Kentico [assembly: RegisterModule(typeof(CustomMFAModule))] public class CustomMFAModule : Module { // Match your module's code name from Kentico admin here public CustomMFAModule() : base("MFAuthentication") { } protected override void OnInit() { base.OnInit(); // Bind the AuthenticationSuccess event to our custom handler AuthenticationEvents.AuthenticationSuccess.Execute += AuthenticationSuccess_Execute; } private void AuthenticationSuccess_Execute(object sender, AuthenticationEventArgs e) { // Get the fully authenticated user object UserInfo authenticatedUser = e.User; if (authenticatedUser != null) { // Disable MFA requirement for this user post-verification authenticatedUser.SetValue("UserMFRequired", false); // Update last logon timestamp (Kentico may auto-update this, but explicit setting ensures consistency) authenticatedUser.UserLastLogon = DateTime.Now; // Persist the changes to the CMS database UserInfoProvider.SetUserInfo(authenticatedUser); } } }
Key Notes
- Skip early-stage events: Events like
Authenticatefire during the authentication process, before your MFA step completes—using those would update fields before the user has passed MFA, which defeats the purpose of your custom verification flow. - Built-in field behavior: The
UserLastLogonfield is often updated automatically by Kentico, but explicitly setting it gives you control if you need custom timing or additional logic tied to the logon event. - Module consistency: Double-check that the module code name (
"MFAuthentication") matches the one you've configured in Kentico's admin panel for your custom MFA module.
内容的提问来源于stack exchange,提问作者Radek Botikova

