本指南介紹的功能,說明您可以在裝置政策控制器 (DPC) 應用程式中實作的安全管理功能。本文件包含程式碼範例,您也可以使用 Test DPC 應用程式,做為 Android 企業功能的程式碼範例來源。
DPC 應用程式可在個人裝置上以設定檔擁有者模式執行,或在全代管裝置上以裝置擁有者模式執行。下表列出 DPC 在設定檔擁有者模式或裝置擁有者模式下執行的可用功能:
| 功能 | 設定檔擁有者 | 裝置擁有者 |
|---|---|---|
| 停用應用程式存取權 | ✓ | ✓ |
| 禁止安裝不明來源的應用程式 | ✓ | ✓ |
| 在 Google Play 限制帳戶 | ✓ | ✓ |
| 啟用企業專用恢復原廠設定保護機制 | ✓ | |
| 監控企業程序記錄和遠端錯誤報告 | ✓ | |
| 授予及移除用戶端憑證存取權 | ✓ | ✓ |
| 安全重設密碼 | ✓ | ✓ |
| 工作資料夾安全驗證問題 | ✓ |
停用應用程式存取權
如果機構想禁止員工在一天中的特定時段或一週中的特定幾天,使用 Android 裝置玩遊戲或觀看 YouTube,DPC 可以暫時停用應用程式存取權。
如要停用應用程式存取權,在裝置擁有者或設定檔擁有者模式下執行的 DPC 會設定 setPackagesSuspended(),然後所選應用程式會像停用一樣 (Google 啟動器會將應用程式設為灰色)。使用者輕觸應用程式時,會看到系統對話方塊,說明應用程式已暫停。
應用程式暫停時,無法啟動活動,且系統會隱藏套件的通知。暫停的套件不會顯示在總覽畫面中,無法顯示對話方塊 (包括浮動式訊息和 Snackbar),也無法播放音訊或震動裝置。
啟動器可以呼叫 isPackageSuspended() 方法,確認應用程式是否已暫停。如要進一步瞭解如何設定應用程式暫停功能,請參閱 setPackagesSuspended。
封鎖不明來源的應用程式
如果應用程式不是從 Google Play (或其他可信任的應用程式商店) 安裝,就稱為不明來源應用程式。使用者安裝這些應用程式後,裝置和資料可能面臨更高的風險。
如要防止使用者安裝來源不明的應用程式,全代管裝置和工作資料夾的管理員元件可以新增 DISALLOW_INSTALL_UNKNOWN_SOURCES 使用者限制。
工作資料夾裝置層級限制
當工作資料夾的管理員新增 DISALLOW_INSTALL_UNKNOWN_SOURCES 時,限制只會套用至工作資料夾。不過,工作資料夾的管理員可以為 Google Play 設定受管理設定,對整個裝置進行限制。如果裝置搭載 Android 8.0 以上版本,且安裝的 Google Play 應用程式版本為 80812500 以上,則可使用裝置層級限制。
如要限制只能透過 Google Play 安裝應用程式,請按照下列步驟操作:
- 為 Google Play 套件
com.android.vending設定受管理設定套件。 - 在套件中,為
verify_apps:device_wide_unknown_source_block鍵設定布林值。 - 新增
ENSURE_VERIFY_APPS使用者限制。
以下範例說明如何檢查 Google Play 是否支援這項設定,並將值設為 true:
Kotlin
internal val DEVICE_WIDE_UNKNOWN_SOURCES = "verify_apps:device_wide_unknown_source_block" internal val GOOGLE_PLAY_APK = "com.android.vending" // ... // Add the setting to Google Play's existing managed config. Supported in // Google Play version 80812500 or higher--older versions ignore unsupported // settings. val dpm = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager var existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK) val newConfig = Bundle(existingConfig) newConfig.putBoolean(DEVICE_WIDE_UNKNOWN_SOURCES, true) dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig) // Make sure that Google Play Protect verifies apps. dpm.addUserRestriction(adminName, UserManager.ENSURE_VERIFY_APPS) dpm.addUserRestriction(adminName, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
Java
static final String DEVICE_WIDE_UNKNOWN_SOURCES = "verify_apps:device_wide_unknown_source_block"; static final String GOOGLE_PLAY_APK = "com.android.vending"; // ... // Add the setting to Google Play's existing managed config. Supported in // Google Play version 80812500 or higher--older versions ignore unsupported // settings. DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); Bundle existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK); Bundle newConfig = new Bundle(existingConfig); newConfig.putBoolean(DEVICE_WIDE_UNKNOWN_SOURCES, true); dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig); // Make sure that Google Play Protect verifies apps. dpm.addUserRestriction(adminName, UserManager.ENSURE_VERIFY_APPS); dpm.addUserRestriction(adminName, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
系統設定中的使用者介面仍會保持啟用狀態,但系統會封鎖應用程式安裝。這項限制會影響日後的安裝作業,先前安裝版應用程式仍會保留在裝置上。裝置使用者可以繼續使用 Android Debug Bridge (ADB),將安裝版應用程式安裝到個人資料夾。
如要進一步瞭解不明來源,請參閱「替代發行選項」。
限制 Google Play 帳戶
有時,組織可能希望允許使用者新增個人 Google 帳戶 (例如在 Gmail 中讀取郵件),但不希望個人帳戶安裝應用程式。DPC 可設定使用者能在 Google Play 中使用的帳戶清單。
全代管裝置或工作資料夾的管理員元件,可以為 Google Play 設定受管理設定,藉此限制帳戶。如要使用帳戶限制功能,安裝的 Google Play 應用程式必須是 80970100 以上版本。
如要限制 Google Play 中的帳戶,請按照下列步驟操作:
- 為 Google Play 套件
com.android.vending設定受管理設定套件。 - 在套件中,將以半形逗號分隔的電子郵件地址,設為
allowed_accounts鍵的字串值。
以下範例說明如何限制帳戶:
Kotlin
internal val ALLOWED_ACCOUNTS = "allowed_accounts" internal val GOOGLE_PLAY_APK = "com.android.vending" // ... // Limit Google Play to one work and one personal account. Use // a comma-separated list of account email addresses (usernames). val googleAccounts = "ali@gmail.com,ali.connors@example.com" // Supported in Google Play version 80970100 or higher. val existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK) val newConfig = Bundle(existingConfig) newConfig.putString(ALLOWED_ACCOUNTS, googleAccounts) dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig)
Java
static final String ALLOWED_ACCOUNTS = "allowed_accounts"; static final String GOOGLE_PLAY_APK = "com.android.vending"; // ... // Limit Google Play to one work and one personal account. Use // a comma-separated list of account email addresses (usernames). String googleAccounts = "ali@gmail.com,ali.connors@example.com"; // Supported in Google Play version 80970100 or higher. Bundle existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK); Bundle newConfig = new Bundle(existingConfig); newConfig.putString(ALLOWED_ACCOUNTS, googleAccounts); dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig);
如要將 Google Play 限制為僅限工作帳戶,請在 DPC 知道帳戶的電子郵件地址後,立即將 allowed_accounts 設為單一受管理帳戶。如果字串空白,使用者就無法在 Google Play 使用任何帳戶。
啟用企業專用恢復原廠設定保護機制
機構可以使用企業專用恢復原廠設定保護機制,指定哪些 Google 帳戶可以佈建已恢復原廠設定的裝置。
消費者恢復原廠設定保護機制旨在防止裝置遭竊。在未經授權恢復原廠設定 (例如使用 EMM) 後,設定精靈會要求使用者驗證先前在裝置個人資料夾中的任何 Google 帳戶,然後才允許任何人佈建裝置。
在企業環境中,當員工離職時,恢復原廠設定是管理員工裝置的重要工具。不過,如果組織不知道員工的帳戶憑證,恢復原廠設定保護機制可能會阻止組織將裝置發給其他員工。
控管恢復原廠設定後的佈建作業
在裝置擁有者模式下執行時,DPC 可以使用 setFactoryResetProtectionPolicy(),控管哪些帳戶有權在恢復原廠設定後佈建裝置。如果這項設定設為 null 或空白清單,則裝置恢復原廠設定後,只有裝置個人資料夾中的帳戶有權佈建裝置。
DPC 可以在全代管裝置的整個生命週期內設定這些帳戶。
- IT 管理員可以使用 People API 的
people.get方法, 並搭配特殊值me。這會擷取已登入帳戶的userId。userID會以people/[userId]形式,在resourceName鍵中以整數字串傳回。新建立的帳戶可能在 72 小時內無法用於恢復原廠設定。 - 您也可以授權一或多位 IT 管理員,在恢復原廠設定後解鎖裝置。請每位 IT 管理員登入 Google 帳戶,並按照步驟 1 操作,將
userId提供給您,以便您在下一個步驟中將這些userIds新增至清單。 - DPC 會使用
setFactoryResetProtectionPolicy()設定適當的應用程式限制,以設定可佈建恢復原廠設定裝置的userId清單。 - DPC 會傳送廣播
com.google.android.gms.auth.FRP_CONFIG_CHANGED做為明確意圖,啟用可在恢復原廠設定後佈建裝置的帳戶,避免因背景限制而遭到捨棄。
Kotlin
const val ACTION_FRP_CONFIG_CHANGED = "com.google.android.gms.auth.FRP_CONFIG_CHANGED" const val GMSCORE_PACKAGE = "com.google.android.gms" // ... // List of userId that can provision a factory reset device. // You can use the value returned calling people/me endpoint. val accountIds = listOf("000000000000000000000") dpm.setFactoryResetProtectionPolicy( adminName, FactoryResetProtectionPolicy.Builder() .setFactoryResetProtectionAccounts(accountIds) .setFactoryResetProtectionEnabled(true) .build() ) val frpChangedIntent = Intent(ACTION_FRP_CONFIG_CHANGED) frpChangedIntent.setPackage(GMSCORE_PACKAGE) context.sendBroadcast(frpChangedIntent)
Java
static final String ACTION_FRP_CONFIG_CHANGED = "com.google.android.gms.auth.FRP_CONFIG_CHANGED"; static final String GMSCORE_PACKAGE = "com.google.android.gms"; // ... // List of userId that can provision a factory reset device. // You can use the value returned calling people/me endpoint. List<String> accountIds = new ArrayList<String>(); accountIds.add("000000000000000000000"); dpm.setFactoryResetProtectionPolicy( adminName, new FactoryResetProtectionPolicy.Builder() .setFactoryResetProtectionAccounts(accountIds) .setFactoryResetProtectionEnabled(true) .build()); Intent frpChangedIntent = new Intent(ACTION_FRP_CONFIG_CHANGED); frpChangedIntent.setPackage(GMSCORE_PACKAGE); context.sendBroadcast(frpChangedIntent);
舊版
如果裝置無法使用 API 級別 30 導入的 setFactoryResetProtectionPolicy(),DPC 可以使用 setApplicationRestrictions,將所選帳戶新增至 com.google.android.gms 套件的 factoryResetProtectionAdmin 受管理設定。
Kotlin
const val GOOGLE_PLAY_APK = "com.android.vending" const val FACTORY_RESET_PROTECTION_ADMIN = "factoryResetProtectionAdmin" const val DISABLE_FACTORY_RESET_PROTECTION_ADMIN = "disableFactoryResetProtectionAdmin" const val GMSCORE_PACKAGE = "com.google.android.gms" // ... val existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK) val newConfig = Bundle(existingConfig) newConfig.putBoolean(DISABLE_FACTORY_RESET_PROTECTION_ADMIN, false) newConfig.putString(FACTORY_RESET_PROTECTION_ADMIN, googleAccounts) dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig) val frpChangedIntent = Intent(ACTION_FRP_CONFIG_CHANGED) frpChangedIntent.setPackage(GMSCORE_PACKAGE) context.sendBroadcast(frpChangedIntent)
Java
static final String GOOGLE_PLAY_APK = "com.android.vending"; static final String FACTORY_RESET_PROTECTION_ADMIN = "factoryResetProtectionAdmin"; static final String DISABLE_FACTORY_RESET_PROTECTION_ADMIN = "disableFactoryResetProtectionAdmin"; static final String GMSCORE_PACKAGE = "com.google.android.gms"; // ... Bundle existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK); Bundle newConfig = new Bundle(existingConfig); newConfig.putBoolean(DISABLE_FACTORY_RESET_PROTECTION_ADMIN, false); newConfig.putStringArray(FACTORY_RESET_PROTECTION_ADMIN, accountIds.toArray(new String[accountIds.size()])); dpm.setApplicationRestrictions(adminName, GOOGLE_PLAY_APK, newConfig); Intent frpChangedIntent = new Intent(ACTION_FRP_CONFIG_CHANGED); frpChangedIntent.setPackage(GMSCORE_PACKAGE); context.sendBroadcast(frpChangedIntent);
停用企業專用恢復原廠設定保護機制
如要停用恢復原廠設定保護機制,DPC 可以使用 setFactoryResetProtectionPolicy() 傳遞 null 值。
Kotlin
const val ACTION_FRP_CONFIG_CHANGED = "com.google.android.gms.auth.FRP_CONFIG_CHANGED" const val GMSCORE_PACKAGE = "com.google.android.gms" // ... dpm.setFactoryResetProtectionPolicy(adminName, null) val frpChangedIntent = Intent(ACTION_FRP_CONFIG_CHANGED) frpChangedIntent.setPackage(GMSCORE_PACKAGE) context.sendBroadcast(frpChangedIntent)
Java
static final String ACTION_FRP_CONFIG_CHANGED = "com.google.android.gms.auth.FRP_CONFIG_CHANGED"; static final String GMSCORE_PACKAGE = "com.google.android.gms"; // ... dpm.setFactoryResetProtectionPolicy(adminName, null); Intent frpChangedIntent = new Intent(ACTION_FRP_CONFIG_CHANGED); frpChangedIntent.setPackage(GMSCORE_PACKAGE); context.sendBroadcast(frpChangedIntent);
舊版
對於無法使用 setFactoryResetProtectionPolicy() (API 級別 30 推出) 的裝置,DPC 可以使用 setApplicationRestrictions,在 com.google.android.gms 套件的 disableFactoryResetProtectionAdmin 受管理設定中設定 true 的索引鍵值。
Kotlin
const val GOOGLE_PLAY_APK = "com.android.vending" const val FACTORY_RESET_PROTECTION_ADMIN = "factoryResetProtectionAdmin" const val DISABLE_FACTORY_RESET_PROTECTION_ADMIN = "disableFactoryResetProtectionAdmin" const val GMSCORE_PACKAGE = "com.google.android.gms" // ... val existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK) val newConfig = Bundle(existingConfig) newConfig.putBoolean(DISABLE_FACTORY_RESET_PROTECTION_ADMIN, true) dpm.setApplicationRestrictions( adminName, GOOGLE_PLAY_SERVICES_PACKAGE, restrictions ) val frpChangedIntent = Intent(ACTION_FRP_CONFIG_CHANGED) frpChangedIntent.setPackage(GMSCORE_PACKAGE) context.sendBroadcast(frpChangedIntent)
Java
static final String GOOGLE_PLAY_APK = "com.android.vending"; static final String FACTORY_RESET_PROTECTION_ADMIN = "factoryResetProtectionAdmin"; static final String DISABLE_FACTORY_RESET_PROTECTION_ADMIN = "disableFactoryResetProtectionAdmin"; static final String GMSCORE_PACKAGE = "com.google.android.gms"; // ... Bundle existingConfig = dpm.getApplicationRestrictions(adminName, GOOGLE_PLAY_APK); Bundle newConfig = new Bundle(existingConfig); newConfig.putBoolean(DISABLE_FACTORY_RESET_PROTECTION_ADMIN, true); dpm.setApplicationRestrictions( adminName, GOOGLE_PLAY_SERVICES_PACKAGE, restrictions); Intent frpChangedIntent = new Intent(ACTION_FRP_CONFIG_CHANGED); frpChangedIntent.setPackage(GMSCORE_PACKAGE); context.sendBroadcast(frpChangedIntent);
監控企業程序記錄和遠端錯誤報告
在 EMM 控制台中,管理員可以使用企業程序記錄和遠端錯誤報告,監控全代管裝置。
記錄企業裝置活動
在裝置擁有者模式下執行的 DPC 可透過遠端追蹤裝置活動 (包括應用程式啟動、Android Debug Bridge (ADB) 活動和螢幕解鎖),找出可疑活動。處理記錄不需要使用者同意。
如要啟用或停用記錄功能,DPC 會呼叫 setSecurityLoggingEnabled()。
當有新的一批記錄可用時,DeviceAdminReceiver 會收到 onSecurityLogsAvailable() 回呼。如要擷取記錄 (收到回呼後),DPC 會呼叫 retrieveSecurityLogs()。
DPC 也可以呼叫 retrievePreRebootSecurityLogs(),擷取前一個重新啟動週期產生的安全性記錄。這是指裝置上次重新啟動與前一次重新啟動之間的時間間隔。不支援 retrieveSecurityLogs() 的裝置會傳回 null。如果應用程式同時使用 retrievePreRebootSecurityLogs() 和 retrieveSecurityLogs() 擷取記錄,則需要檢查是否有重複項目。
注意:這項功能只會記錄完全受管理裝置 (由裝置擁有者或其 DELEGATION_SECURITY_LOGGING 委派者管理) 的活動,且裝置上只有單一使用者或關聯使用者。這項功能會記錄整部裝置的活動,因此不適用於個人裝置。
這項設定會記錄下列類型的動作,因此在安全性事件發生後,可用於稽核:
- 每次應用程式重新啟動時。這有助於判斷是否有惡意軟體從遭入侵的應用程式開始。
- 裝置解鎖失敗。這項功能可判斷短時間內是否有多起解鎖失敗事件。
- 使用者透過 USB 傳輸線將裝置連接到電腦時,可能有害的 ADB 指令。
如要進一步瞭解如何解讀記錄,請參閱 SecurityLog。
在開發和測試期間,您可以強制系統向 DPC 提供任何現有的安全性記錄,不必等待完整批次。在 Android 9.0 (API 級別 28) 以上版本中,請在終端機中執行下列 Android Debug Bridge (adb) 指令:
adb shell dpm force-security-logs
系統會限制工具的使用頻率,並在終端機輸出內容中回報任何刻意減速的情況。如果有可用的記錄,DPC 會收到 onSecurityLogsAvailable() 回呼。
從遠端要求錯誤報告
在裝置擁有者模式下執行的 DPC,可以遠端要求使用者裝置的錯誤報告,但裝置只能有一位使用者或關聯使用者。錯誤報告會擷取要求錯誤報告當下的裝置活動,但也可能包含前幾小時的活動,具體取決於 logcat 緩衝區重新整理的頻率。
如要從遠端要求錯誤報告,DPC 會呼叫 requestBugreport():
- 如果使用者接受分享錯誤報告,DPC 會使用
onBugreportShared()接收錯誤報告。 - 如果使用者拒絕分享錯誤報告,DPC 會使用
onBugreportSharingDeclined()收到分享要求遭拒的訊息。 - 如果錯誤報告失敗,DPC 會看到
onBugreportFailed(),並顯示BUGREPORT_FAILURE_FAILED_COMPLETING或BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE。
授予及移除用戶端憑證存取權
如果以設定檔擁有者或裝置擁有者模式執行的 DPC 授予第三方應用程式管理憑證的能力,則該應用程式可以自行授予其安裝的憑證存取權,而無需使用者介入。如要安裝設定檔中所有應用程式都能存取的憑證,請使用 installKeyPair()。
如要瞭解應設定哪些參數,請參閱 installKeyPair()。這項功能會與現有的憑證管理 API 搭配運作。
部署情境
不使用 installKeyPair() 方法:
- 使用者每次要授予憑證存取權時,都必須輕觸憑證名稱,然後輕觸「Allow」(允許)。
- 使用者在安裝憑證時會看到提示,且必須為憑證命名。
使用 installKeyPair() 方法:
- 使用者不必在每次要授予憑證存取權時輕觸「Allow」(允許)。
- 使用者無法重新命名憑證。
- 管理員擁有更多控制權,可以封鎖不應存取特定憑證的應用程式憑證。
移除用戶端憑證
授予用戶端憑證存取權後,如要從遠端移除透過 installKeyPair() 安裝的用戶端憑證,請呼叫 removeKeyPair()。
在裝置擁有者模式或設定檔擁有者模式下執行的 DPC,或委派的憑證安裝程式,可以呼叫 removeKeyPair()。這會移除在指定私密金鑰別名下安裝的憑證和私密金鑰組。
部署情境
如果組織要改用更安全的用戶端憑證,請使用這項功能。如果管理員推出新憑證,且發布作業需要很長的時間,管理員可以在遷移完成後撤銷已淘汰的憑證。
安全重設密碼
DPC 可以使用預先註冊的安全權杖授權變更,重設使用者的密碼。裝置擁有者和設定檔擁有者可以呼叫安全密碼重設 API,分別變更裝置和工作資料夾的密碼。安全密碼重設功能取代了 resetPassword(),並提供下列改善項目:
如果 DPC 版本指定 Android 8.0 (API 級別 26) 以上版本,請使用安全密碼重設功能。在指定 Android 8.0 以上版本的 DPC 中,呼叫 resetPassword() 會擲回 SecurityException,因此您可能需要更新 DPC。
設定及啟用權杖
DPC 必須先設定並啟用權杖,才能重設密碼。由於 DPC 可能無法立即使用權杖,因此您必須在 IT 管理員可能需要使用權杖之前設定權杖。
重設密碼權杖是加密強度高的隨機值,長度至少須為 32 個位元組。請為每部裝置和設定檔建立權杖,不要重複使用或共用產生的權杖。
建議您將權杖或解密加密權杖的方法儲存在伺服器上。如果您將權杖儲存在本機的憑證加密儲存空間,DPC 必須等到使用者解鎖裝置或設定檔後,才能重設密碼。如果將權杖儲存在裝置加密儲存空間,而該空間遭到入侵,攻擊者可能會使用權杖存取工作資料夾或主要使用者。
您可以在 DPC 中產生新權杖,或從伺服器擷取權杖。以下範例顯示 DPC 自行產生權杖並回報給伺服器:
Kotlin
val token = ByteArray(32) // Generate a new token val random = SecureRandom() random.nextBytes(token) // Set the token to use at a later date val success: Boolean success = dpm.setResetPasswordToken(DeviceAdminReceiver.getComponentName(context), token) // Activate the token and update success variable... // Store the token on a server if (success) { sendTokenToServer(token) }
Java
byte token[] = new byte[32]; // Minimum size token accepted // Generate a new token SecureRandom random = new SecureRandom(); random.nextBytes(token); // Set the token to use at a later date boolean success; success = dpm.setResetPasswordToken(DeviceAdminReceiver.getComponentName(getContext()), token); // Activate the token and update success variable ... // Store the token on a server if (success) { sendTokenToServer(token); }
在大多數情況下,DPC 必須在設定權杖後啟用權杖。不過,如果使用者未設定螢幕鎖定密碼,系統會立即啟用權杖。如要啟用權杖,請要求使用者確認憑證。DPC 可以呼叫 KeyguardManager 方法 createConfirmDeviceCredentialIntent(),取得啟動確認程序的 Intent。在使用者介面中向裝置使用者說明,為何要求他們進行驗證。下列程式碼片段說明如何在 DPC 中啟用權杖:
Kotlin
// In your DPC, you'll need to localize the user prompt val ACTIVATE_TOKEN_PROMPT = "Use your credentials to enable remote password reset" val ACTIVATE_TOKEN_REQUEST = 1 // Create or fetch a token and set it in setResetPasswordToken() ... val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager val confirmIntent = keyguardManager.createConfirmDeviceCredentialIntent(null, ACTIVATE_TOKEN_PROMPT) if (confirmIntent != null) { startActivityForResult(confirmIntent, ACTIVATE_TOKEN_REQUEST) // Check your onActivityResult() callback for RESULT_OK } else { // Null means the user doesn't have a lock screen so the token is already active. // Call isResetPasswordTokenActive() if you need to confirm }
Java
// In your DPC, you'll need to localize the user prompt static final String ACTIVATE_TOKEN_PROMPT = "Use your credentials to enable remote password reset"; static final int ACTIVATE_TOKEN_REQUEST = 1; // Create or fetch a token and set it in setResetPasswordToken() ... KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); Intent confirmIntent = keyguardManager.createConfirmDeviceCredentialIntent( null, ACTIVATE_TOKEN_PROMPT); if (confirmIntent != null) { startActivityForResult(confirmIntent, ACTIVATE_TOKEN_REQUEST); // Check your onActivityResult() callback for RESULT_OK } else { // Null means the user doesn't have a lock screen so the token is already active. // Call isResetPasswordTokenActive() if you need to confirm }
您必須先啟用 DPC 設定的權杖,裝置才能重新啟動。Android 會將未啟用的權杖儲存在記憶體中,且不會在重新啟動後保留權杖。如果使用者在啟用權杖前重新啟動裝置,DPC 可以再次設定相同的權杖,或產生新的權杖。
DPC 可以呼叫 isResetPasswordTokenActive() 並檢查結果是否為 true,確認權杖是否有效。
DPC 設定並啟用權杖後,權杖會持續有效,直到 DPC 刪除或更換權杖 (或裝置恢復原廠設定) 為止。權杖與密碼無關,不受使用者變更或清除密碼的影響。
刪除權杖
您可以呼叫 clearResetPasswordToken(),刪除 DPC 先前設定的權杖。您可能需要撤銷遭入侵的權杖,或移除重設密碼的功能。以下範例說明如何在 DPC 中執行這項操作:
Kotlin
val dpm = getDpm() val admin = DeviceAdminReceiver.getComponentName(requireActivity()) // Clear the token if (!dpm.clearResetPasswordToken(admin)) { // Report the failure and possibly try later ... }
Java
DevicePolicyManager dpm = getDpm(); ComponentName admin = DeviceAdminReceiver.getComponentName(getActivity()); // Clear the token if (!dpm.clearResetPasswordToken(admin)) { // Report the failure and possibly try later ... }
重設密碼
IT 管理員需要重設密碼時,請呼叫 resetPasswordWithToken(),並傳遞 DPC 預先設定及啟用的權杖:
Kotlin
val token: ByteArray = getTokenFromServer() val newPassword = "password" try { val result: Boolean = dpm.resetPasswordWithToken( DeviceAdminReceiver.getComponentName(requireContext()), newPassword, token, 0 ) if (result) { // The password is now 'password' } else { // Using 'password' doesn't meet password restrictions } } catch (e: IllegalStateException) { // The token doesn't match the one set earlier. }
Java
byte token[] = getTokenFromServer(); String newPassword = "password"; try { boolean result = dpm.resetPasswordWithToken( DeviceAdminReceiver.getComponentName(getContext()), newPassword, token, 0); if (result) { // The password is now 'password' } else { // Using `password` doesn't meet password restrictions } } catch (IllegalStateException e) { // The token doesn't match the one set earlier. }
如果新密碼不符合下列限制,呼叫 resetPasswordWithToken() 會傳回 false,且密碼不會變更:
- 字元數符合密碼長度下限限制。呼叫
getPasswordMinimumLength(),瞭解 IT 管理員是否設定長度限制。 - 密碼中的字元範圍和複雜度符合組合限制。呼叫
getPasswordQuality(),瞭解 IT 管理員是否設定了組合限制。
如果密碼品質限制不要求設定密碼,您可以將 null 或空字串傳遞至 resetPasswordWithToken(),以移除密碼。
工作資料夾安全驗證問題
在設定檔擁有者模式下執行的 DPC,可以要求使用者為工作資料夾中執行的應用程式指定安全性驗證。當使用者嘗試開啟任何工作應用程式時,系統會顯示安全性驗證。如果使用者成功完成安全性驗證,系統就會解鎖工作資料夾,並視需要解密。
工作資料夾安全驗證問題的運作方式
- 如果 DPC 傳送
ACTION_SET_NEW_PASSWORD意圖,系統會提示使用者設定安全驗證。 - DPC 也可以傳送
ACTION_SET_NEW_PARENT_PROFILE_PASSWORD意圖,提示使用者設定裝置鎖。
DPC 可以為工作驗證設定與其他裝置密碼不同的密碼政策。舉例來說,裝置驗證問題回應的長度下限,可能與其他密碼的長度要求不同。DPC 會使用一般的 DevicePolicyManager 方法設定驗證政策,例如 setPasswordQuality() 和 setPasswordMinimumLength()。
注意事項
- DPC 可以重設工作資料夾的密碼,但無法重設裝置 (個人) 密碼。如果使用者選擇將工作和個人密碼設為相同,則在工作資料夾上執行
resetPassword()只會重設工作資料夾的密碼,且密碼不會與裝置螢幕鎖定的密碼相同。 - DPC 可以使用
setOrganizationColor()和setOrganizationName()自訂工作驗證碼的憑證畫面。 - 裝置管理員無法使用
resetPassword()清除密碼或變更已設定的密碼。裝置管理員仍可設定密碼,但僅限裝置沒有密碼、PIN 碼或圖案時。
詳情請參閱 getParentProfileInstance() 和 DevicePolicyManager 下方的參考文件。