Loading
保護您的 Salesforce 組織
使用 Apex 實作自訂 MFA 流程 (Salesforce 組織)

使用 Apex 實作自訂 MFA 流程 (Salesforce 組織)

若要實作您自己的多因素驗證 (MFA) 流程,請使用 System.UserManagement 類別下的 Apex 方法。Salesforce Authenticator 和電話 (SMS) 身分驗證方法等方法成對出現,一個啟動驗證服務,另一個完成驗證服務。針對密碼或時間型一次性密碼 (TOTP) 驗證方法,您可以單獨使用第二個方法來提供完整的驗證服務。僅支援存取貴公司 Experience Cloud 網站、員工社群和其他類型社群入口網頁的外部使用者使用 SMS 進行 MFA。

必要版本

提供版本:Salesforce Classic 與 Lightning Experience
提供版本:所有版本
重要
重要

MFA 需求會在 2026 年 6 月變更。

您使用的 Apex 組態取決於您的驗證方法。

  • 若要針對 Salesforce Authenticator 或 SMS 驗證方法實作 MFA 服務,請使用 initVerificationMethod 和 verifyVerificationMethod。

  • 若要針對密碼或 TOTP 驗證方法實作 MFA 服務,請使用 verifyVerificationMethod。

範例
範例 MFA 的 Apex 方法

此範例顯示使用 Salesforce Authenticator 作為驗證方法的 MFA Apex 程式碼。

public void initVerification() {
// user will receive push notification on mobile device where the app is registered for MFA
 identifier = UserManagement.initVerificationMethod(Auth.VerificationMethod.SALESFORCE_AUTHENTICATOR);
}

public Auth.VerificationResult verifyVerification() {
// requiring identifier from the initVerification
// user will need to take the action on the mobile device where the app is registered for MFA
return UserManagement.verifyVerificationMethod(identifier, '' , Auth.VerificationMethod.SALESFORCE_AUTHENTICATOR);
}

以下是 SMS 的範例。

public void initVerification() {
// user will receive code on their registered verified phone
 identifier = UserManagement.initVerificationMethod(Auth.VerificationMethod.SMS);
}

public Auth.VerificationResult verifyVerification() {
// requiring identifier from the initVerification
// the code will need to be entered in this method
return UserManagement.verifyVerificationMethod(identifier, code , Auth.VerificationMethod.SMS);
}

後續兩個範例顯示僅使用密碼和 TOTP 驗證之 verifyVerificationMethod 的 MFA Apex 程式碼。

public Auth.VerificationResult verifyVerification() {
// user will enter their password as a param in the verifyVerificationMethod for password verification method
return UserManagement.verifyVerificationMethod('', password , Auth.VerificationMethod.PASSWORD);
}
public Auth.VerificationResult verifyVerification() {
// user will enter their registered time-based one-time password (TOTP) code (token)
return UserManagement.verifyVerificationMethod('', code , Auth.VerificationMethod.TOTP);
}
 
正在載入
Salesforce Help | Article